Regex find and replace in Google Docs: what works, what doesn't, and how to use capture groups
Google Docs can find with regular expressions: tick Match using regular expressions in Edit > Find and replace. It cannot replace with them. Google's help page (checked 1 November 2026) says: "Regular expression replacements (including capture groups and special characters like \n for newlines) only work with Google Sheets. In Google Docs, any regex formatting entered into the "Replace with" field will be inserted as literal text." So $1 in Replace with puts the two characters "$1" into your document. Below: what the Docs dialog does handle, patterns that work as wildcards, and three ways to get capture-group replaces anyway.
Docs vs Sheets, side by side
| Google Docs | Google Sheets | |
|---|---|---|
| Regex in the Find box | Yes ("Match using regular expressions") | Yes ("Search using regular expressions") |
Capture groups $1, $2 in Replace with | No: inserted as literal text | Yes |
\n (new line) in Replace with | No: inserted as literal text | Yes |
^ and $ anchors | Google's examples mark them "only works with Google Sheets" | Yes (start / end of a cell) |
Lookahead (?=…), lookbehind, backreferences \1 | No (RE2 does not support them) | No (RE2) |
| Regex flavour | RE2, Google's regex library, in both | |
Wildcards: patterns that work in the Docs Find box
Docs has no Word-style wildcard mode; with regex ticked, these do the same job. Every pattern below was checked on sample text with node on 1 November 2026, and uses only syntax that means the same in RE2.
| To find | Find (regex on) | Matches |
|---|---|---|
| Either spelling | colou?r | color, colour |
| A whole word only | \bcat\b | cat, not concat or cats |
| Any text in brackets | \(.*?\) | (one), (two): .*? is the shortest run, .* would take "(one) b (two)" as one match |
| Double spaces | \s{2,} | 2 or more spaces in a row (Do it on pasted text: collapse double spaces) |
| Acronyms | [A-Z]{2,} | NASA, EU (tick Match case) |
| US zip codes | [0-9]{5}(-[0-9]{4})? | 90210, 10001-1234 |
| Web addresses | https?://\S+ | https://x.io/a, http://y.org |
| Percentages | \d+(\.\d+)?% | 12%, 3.5% |
Put a backslash before a character that has a regex meaning to find it literally: \$, \., \(. With a pattern in Find you can still Replace all with plain text, which covers "replace every variant with one word" (all of colou?r to "color"). It is the reuse part of the match that Docs can't do.
Capture groups in Docs: three ways
1. Through Google Sheets or a text editor (plain text, loses formatting)
For a list of lines (names, dates, references), paste it into one column of a Google Sheet, run Edit > Find and replace there with "Search using regular expressions" and $1-style replacements, and paste the column back. A text editor with regex replace (VS Code, Notepad++) does the same on any text; our line-break guide walks through it. Bold, links and headings in that text are lost on the way out and back.
Many patterns at once on plain text: our free bulk find and replace tool runs a list of regex pairs with $1 groups in your browser and shows each pair's matches before it replaces.
2. Apps Script, inside the document
Open Extensions > Apps Script in your document. Docs' own script method replaceText takes an RE2 pattern but a plain replacement, so it is fine for "every variant to one word":
// RE2 pattern as a string, plain replacement text (no $1).
function tidySpelling() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText('colou?r', 'color');
body.replaceText('gr[ae]y', 'gray');
}
Google's Apps Script reference says capture groups are not fully supported there. For $1, $2, use JavaScript's own regex on each paragraph's text and rewrite only the matched characters, so the rest of the paragraph keeps its formatting. Paste this, choose myFunction, click Run, and allow access on the first run:
// Regex replace with capture groups ($1, $2 ... and $$ for a dollar sign),
// one paragraph at a time, using JavaScript's RegExp (Apps Script V8).
// Only the matched characters are rewritten; other formatting stays.
function regexReplace(re, replacement) { // re needs the g flag
var paras = DocumentApp.getActiveDocument().getBody().getParagraphs();
paras.forEach(function (p) {
var text = p.editAsText();
var s = text.getText();
var hits = [];
var m;
re.lastIndex = 0;
while ((m = re.exec(s)) !== null) {
if (m[0] === '') { re.lastIndex++; continue; }
var out = replacement.replace(/\$(\$|\d{1,2})/g, function (all, tok) {
return tok === '$' ? '$' : (m[+tok] || '');
});
hits.push({ at: m.index, end: m.index + m[0].length - 1, out: out });
}
for (var i = hits.length - 1; i >= 0; i--) {
if (hits[i].out) text.insertText(hits[i].end + 1, hits[i].out);
text.deleteText(hits[i].at, hits[i].end);
}
});
}
function myFunction() {
regexReplace(/(\w+), (\w+)/g, '$2 $1'); // "Smith, John" -> "John Smith"
// regexReplace(/(\d{1,2})\/(\d{1,2})\/(\d{4})/g, '$3-$1-$2'); // 3/8/2015 -> 2015-3-8
}
Because this is JavaScript's regex, lookahead and lookbehind work too (/(?<=USD )\d+/g). Limits: one paragraph at a time (no match across a paragraph break), the body only (not headers, footers or footnotes), and the first tab of a tabbed document. We have not tested paragraphs that hold images, smart chips or equations, where the text offsets may differ: run it on a copy (File > Make a copy) first.
Tested on 1 November 2026 with node against a small mock of DocumentApp: date and name reordering, $$, a missing group, empty matches, lookbehind, and formatting kept outside the match. Not yet run in a live Google Doc; tell us at replacekit@tuper.app if it behaves differently.
3. ReplaceKit regex mode (no script)
ReplaceKit is a Google Docs add-on we are building. Its Find box takes JavaScript regular expressions with $1…$99, named groups $<name> and $& (the whole match) in Replace with, plus lookahead and lookbehind. \n matches a paragraph break and \v a line break (how that works). You see a preview of every match before replacing, can limit it to the selection, and it covers headers, footers and footnotes. Only the matched characters change, so bold and links elsewhere stay.
Free: all of the above, with no click limit and no trial. Pro ($6/month or $48/year): bulk lists of find => replace pairs, replace with formatting, saved rule sets.
ReplaceKit is built and in testing. It is not in the Google Workspace Marketplace yet. Leave your email and we send one message when you can install it.
The free version will have no click limit or trial clock.
More ReplaceKit guides
- Find and replace with a line break or paragraph break in Google Docs
- How to remove line breaks in Google Docs (PDF paste fix)
- Find and replace formatting in Google Docs (bold, colour, highlight)
- Find and replace in a selection only in Google Docs (and fixes when it is not working)
- Free online tool: remove line breaks from any text (runs in your browser)
Sources
- Google Docs Editors Help: Search and use find and replace (checked 1 Nov 2026: Docs options are "Match case" and "Match using regular expressions"; "Regular expression replacements (including capture groups and special characters like \n for newlines) only work with Google Sheets. In Google Docs, any regex formatting entered into the "Replace with" field will be inserted as literal text."; Google products use RE2)
- Apps Script reference: Body (checked 1 Nov 2026: findText and replaceText use RE2; "A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers"; the pattern is matched against each text block separately)
- Apps Script reference: Text (setBold, setForegroundColor, setBackgroundColor, insertText, deleteText)
- RE2 syntax (lookahead, lookbehind and backreferences: NOT SUPPORTED)