Google Sheets function · Text
REGEXREPLACE in Google Sheets: syntax and examples
REGEXREPLACE finds every part of a text that matches a RE2 pattern and replaces it. Use it to strip characters, reformat codes and dates, or clean input that SUBSTITUTE cannot describe.
Try the pattern first in our RE2 regex tester: it uses RE2 syntax, the same as Google Sheets, and runs in your browser.
Syntax
REGEXREPLACE(text, regular_expression, replacement)
Arguments
| Argument | Type | Required | What it does |
|---|---|---|---|
text | string | yes | The text to change. |
regular_expression | string | yes | A RE2 pattern. Every match is replaced. |
replacement | string | yes | The text that takes the place of each match. Numbered capture groups can be inserted. |
Examples
The examples use this table, starting in A1:
| Text |
| Tel: 0721 234 567 |
| ana.pop@example.com |
| too many spaces |
| Ana Pop |
=REGEXREPLACE(A2, "\D", "")Result: 0721234567
Keep only the digits: every non-digit character is replaced by nothing.
Run in a Google Sheet (English US locale) on .
=REGEXREPLACE(A3, "@.*$", "")Result: ana.pop
Remove everything from the @ onward, leaving the user name.
Run in a Google Sheet (English US locale) on .
=REGEXREPLACE(A4, "\s+", " ")Result: too many spaces
Collapse any run of whitespace into one space.
Run in a Google Sheet (English US locale) on .
=REGEXREPLACE(A5, "(\w+) (\w+)", "$2, $1")Result: Pop, Ana
Swap two words with capture groups: the first group goes after the second.
Run in a Google Sheet (English US locale) on .
=ARRAYFORMULA(REGEXREPLACE(A2:A3, "[aeiou]", "*"))Result:
| T*l: 0721 234 567 |
| *n*.p*p@*x*mpl*.c*m |
Replace vowels in a whole range at once.
Run in a Google Sheet (English US locale) on .
Common errors
- It replaces every match, not just the first. REGEXREPLACE("abab", "a", "x") gives xbxb.
- Matching is case sensitive. Start the pattern with (?i) to ignore case.
- An invalid pattern, such as an unclosed parenthesis, returns #REF!.
- The result is always text. A number passed in comes back as text.
Compared with Excel
Older Excel versions have no REGEXREPLACE. The regex functions added to Microsoft 365 use a different pattern flavour, so test patterns before copying them.