Quick Links
When you’re working with a large document, it’s often useful to find specific text strings quickly. Google Docs’ Find and Replace feature is a helpful tool, but a hidden feature can make it even more powerful: Regular Expressions.
Maximizing Find and Replace With RegEx
The ability to locate specific text within a document is valuable.Google Docs’ Find and Replace featureenhances it by offering even more convenience. However, you may take your search capabilities to the next level with Regular Expressions (RegEx). RegEx is a powerful tool that uses a sequence of characters to match patterns within text, allowing you to find content with remarkable precision.
In Google Docs, you may use RegEx to refine your search process. While the standard Find feature only lets you search for fixed text, RegEx lets you define patterns, easily locating complex sequences.

Unfortunately, Google Docs does not currently support replacing text with RegEx patterns. So, while you can use RegEx to find specific strings, you can only replace them with fixed text.
Unlike Google Docs, Google Sheets offers a REGEXREPLACE function, which allows for searching and replacing text using RegEx, making it a highly effective tool forfinding and replacing in Google Sheets.

Despite this limitation of Google Docs, RegEx can save you significant time, especially when working with large documents. Since the RegEx option is unchecked by default, it’s easy to overlook. However, once you familiarize yourself with its functionality, you’ll discover that RegEx is surprisingly simple and can become an invaluable part of your editing toolkit.
To start using Find and Replace with RegEx in Google Docs, pressCtrl/Cmd+Fto open the Find menu. Next, click the three vertical dots to open the Find and Replace window. Check theUse regular expressionsbox, and you’re ready to go.

If you prefer, you may open the Find and Replace window with a single shortcut:Ctrl + Hon Windows orCmd + Shift + Hon Mac.
Google Docs uses the RE2 syntax for RegEx, and you can review the full syntax guide on theRE2 GitHub pagefor more detailed information. But if you’re unfamiliar with RegEx, the following examples of common uses can help to explain them.

Replace Words With More Precision
One of the common challenges with the standard Find and Replace feature is that it often replaces words within other words. For example, if you want to replace the word “bar” with “pub,” you might accidentally change “bargain” to “pubgain” or “barbershop” to “pubbershop.” This can lead to awkward and incorrect modifications.
This is where RegEx comes to the rescue. Using RegEx, you can specify that you want to find only the exact word “bar” and not instances where “bar” is part of a larger word. In the RE2 RegEx syntax,\bmarks the boundaries of a word, ensuring that the search is limited to the word itself, without affecting other words that contain the same letters.

For this example, the following sequence will find only the word “bar”:
Once you’ve entered this sequence, you may type “pub” in theReplace withbox and confidently clickReplace all, knowing that only the exact matches of the word “bar” will be replaced.
Remove Citation Numbers
Statements need citation numbers to inform the reader where the information comes from and assure them of its validity. However, if you’re quoting from a website, you may not need these citation numbers, as your text won’t include a lengthy reference page. In such cases, citation numbers can clutter your text and make it look messy.
With RegEx, you’re able to quickly remove these citation numbers in Google Docs, leaving just the text you need. Consider the text below:
Removing the citation numbers and brackets one by one can be tedious. However, the RegEx sequence below can find all citation numbers in one go:
The\d+indicates that we’re looking for one or more digits, and the brackets ([ ]) mean that the digits will be inside brackets. From here, leave theReplace withbox empty and clickReplace allto delete the citation numbers.
Find Duplicate Words
Duplicate words often sneak into our writing, especially after editing. My brain tends to skip over them automatically, making it tough to spot these errors when proofreading. Whilea good grammar checkermight catch duplicate words, you may also use RegEx in Google Docs to find them.
One of RegEx’s cool features is backreferencing, allowing the sequence to remember what it looked for. You can use this to find duplicate consecutive words with RegEx:
The code above marks the word boundaries with\b, indicates a word with\w+, and places this word inside parentheses to create a capturing group so that the word can be referenced later. The\s+represents one or more whitespace characters, and the\1is a backreference to the first captured group (the word itself).
In short, the RegEx sequence looks for a word, followed by whitespace, and then the same word again. This effectively highlights all duplicate words in your document.
If Google Docs supported RegEx for replacements, you could remove all those duplicates in one step. As it is, you’ll need to find each using RegEx, then remove them individually.
Clean Up Web Links
URLs often come with unnecessary tracking parameters, making them long and unattractive. These parameters usually help the website track information like how you found the page, whether you’re logged in, and more. While you may be okay with this tracking, these extra parameters make URLs longer than necessary.
RegEx can help clean up these URLs by removing everything after the question mark, which is typically where the tracking parameters begin:
The sequence starts with a question mark. The dot (.) matches any character, and the plus (+) means one or more of the preceding element (in this case, any character). Although the RegEx sequence successfully highlights the unnecessary parameters in the URLs, there is one minor issue: if you have a sentence with an actual question mark used for punctuation, the RegEx sequence will match it too.
To avoid this, you can tweak the RegEx sequence:
In this sequence, the?!is a negative look-ahead assertion, ensuring that the character immediately following the question mark is not whitespace (\s). This will exclude actual questions from the pattern.
Now you may safely remove the junk from URLs by replacing it with nothing. With cleaner URLs, the table is a lot less cluttered:
Make sure you check your final URLs to see that they still work!
These examples are just a few of the countless ways RegEx can assist you in Google Docs. While they may seem intimidating at first, RegEx are straightforward once you get the hang of them. Start experimenting and you’ll soon discover how much time and effort RegEx can save you.