

Life hacks: Simple search and replace in Google Docs

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"
In most cases, this works wonderfully and the method has been tried and tested for decades. However, users with more technical knowledge or programmers who need to make text mutations in their programmes are familiar with regular expressions. This feature is now supported by Google Docs.
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:
- (.+?) is a regular expression
- The bracket () defines an area in the text that the search function should remember.
- The counterpart to () in the replace field is $1. The content of the brackets is inserted here. If there are two brackets in the search field, then $2 is used.
- The dot, i.e. . stands for "any character", i.e. a letter, a number, a space or a special character
- The plus, i.e. +, stands for "one or more" of the previous character. In technical jargon, this is called a "quantifier". In our case, this means "any one or more characters"
- The question mark, i.e. ?, is also a quantifier. It tells Regular Expression to be lazy. This instructs the search to include as few characters as possible in the search result.
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:
- .?@.+?&..+? - searches for all email addresses in a text, but does not remember them for placeholders because the brackets are missing. The backslash, i.e. ** is a so-called "escape character", which instructs the search not to consider the following character as part of the regular expression, but to search exactly as written.
- rot|er\b|en\b|em\b|es\b - searches for the words rot, roter, roten, rotem and rotes. The vertical line, i.e. | stands for "or". \b stands for "word boundary", as a space, a comma, a full stop or something else.
- Something more abstract. Do you know texts in which people sometimes put two spaces or even more between words? Searching for them is annoying, isn't it? Try " {2,500}" without inverted commas and replace it with " " also without inverted commas. This means that we search for series of spaces, at least 2, at most 500 and replace them with one space.
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.


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.