Your data. Your choice.

If you select «Essential cookies only», we’ll use cookies and similar technologies to collect information about your device and how you use our website. We need this information to allow you to log in securely and use basic functions such as the shopping cart.

By accepting all cookies, you’re allowing us to use this data to show you personalised offers, improve our website, and display targeted adverts on our website and on other websites or apps. Some data may also be shared with third parties and advertising partners as part of this process.

Background information

Life hacks: Simple search and replace in Google Docs

Dominik Bärlocher
29.9.2016
Translation: machine translated

Google Docs has added a new feature that many have been waiting for. For all those who have no idea what regular expressions are all about and why it's worth getting to know them: a short crash course.

In practically any text editor, you can press CTRL+H on Windows or ⌘+H on MacOS and then Find/Replace. This means you can enter a word and replace it with another one.

Simply put, it works like this:

  • Sentence: "My T-shirt is blue today"
  • CTRL+H / ⌘+H
  • Search for: blue
  • Replace with: red
  • OK
  • New is the sentence "My T-shirt is red today"

Regular expressions? What is that?

Regular expressions are abstractions of text or text constructs. In addition, there are some so-called modifiers as well as options for text analysis. Regular expressions are extremely powerful and not only practical for programmers. Regular expressions are by far the most useful and versatile thing I have learnt about computers in the last three years.

Let's take a slightly more complex sentence with some HTML formatting:

I'm wearing a red T-shirt in Rotterdam.

The code italicises the words "rotes" and "Rotterdam". However, we want the two words to be bold - expressed in HTML with . With the above method, it would work like this:

  • CTRL+H / ⌘+H
  • Search for:
  • Replace with:
  • OK
  • CTRL+H / ⌘+H
  • Search for: </em>
  • Replace with: </strong>
  • OK

Too much effort. It's easier with regular expressions:

  • Search for: (.+?)
  • Replace with: $1

Moment... what was that?

What I did in the example above is simply explained:

  • I passed a placeholder to the search with (.+?)
  • This placeholder was ignored by the search and everything around it was replaced
  • But so that I don't lose the text that is important for the placeholder, I have inserted it with $1 at the place of my choice

The strange symbols can be explained as follows:

The result:

I'm wearing a red T-shirt in Rotterdam.

If we had not included the quantifier of the question mark in the regular expression, the result would have been the following:

I am wearing a red</em> T-shirt in Rotterdam.

Regular expressions can do infinitely more. But to explain this would be presumptuous and would go beyond the scope of a short news item about "Google Docs can do this now". Just a few examples:

Curious? Then visit RegExr for an easy-to-understand and complete reference for all regular expressions. If you want to test your regular expressions without ruining your text, then Rubular is a good option.

Back to Google Docs

Since Thursday, Google Docs supports regular expressions. Google is thus doing a favour to many people who perhaps write their texts in a more code-heavy way. In general, however, regular expressions are something that gives every user great power and can make life very, very easy.

Have fun! And don't forget, if you completely mess it up, CTRL+Z or ⌘+Z is your friend.

13 people like this article


User Avatar
User Avatar

Journalist. Author. Hacker. A storyteller searching for boundaries, secrets and taboos – putting the world to paper. Not because I can but because I can’t not.


Background information

Interesting facts about products, behind-the-scenes looks at manufacturers and deep-dives on interesting people.

Show all