REGEXREPLACE Function
Replaces text that matches a regular expression pattern.
Category
Text
Available in
Microsoft 365
Returns
Text
Want to practice REGEXREPLACE in a real spreadsheet? Open a blank sheet in our Practice Sandbox and try it with live data, with the AI Excel tutor beside you. No Excel needed — just a free account.Try it free in the Sandbox
Overview
REGEXREPLACE is SUBSTITUTE with superpowers: instead of one fixed word, it replaces anything that fits a pattern — every digit, every run of spaces, every phone number.
Use $1, $2 in the replacement to put back parts of the match you captured with brackets.
Syntax
=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
Arguments
| Argument | Required? | What it does |
|---|---|---|
| text | Yes | The original text. |
| pattern | Yes | The regular expression to find. |
| replacement | Yes | What to put in. "" removes the match. |
| occurrence | Optional | 0 = every match (default), 2 = the second, -1 = the last, and so on. |
| case_sensitivity | Optional | 0 = case-sensitive (default), 1 = ignore case. |
Examples
Example 1 — Masking phone numbers
Keep only the last four digits visible.
B2fx=REGEXREPLACE(A2, "\d{3}-\d{3}-(\d{4})", "***-***-$1")
| A | B | |
|---|---|---|
| 1 | Note | Masked |
| 2 | Call 604-555-0199 | Call ***-***-0199 |
| 3 | Mobile: 403-555-0142 | Mobile: ***-***-0142 |
=REGEXREPLACE(A2, "\d{3}-\d{3}-(\d{4})", "***-***-$1") Explain
Example 2 — Digits only
Strip every character that isn't a digit.
B2fx=REGEXREPLACE(A2, "[^0-9]", "")
| A | B | |
|---|---|---|
| 1 | Typed phone | Digits |
| 2 | (604) 555-0199 | 6045550199 |
| 3 | 403.555.0142 | 4035550142 |
=REGEXREPLACE(A2, "[^0-9]", "") Explain
Common mistakes
- Special characters need a backslash. To match a real dot, bracket or plus sign, write \., \( or \+.
Notes
- Related functions: SUBSTITUTE, REPLACE, REGEXEXTRACT, REGEXTEST.

Author
Kyle Stott
Certified Microsoft Excel Expert
Kyle has worked professionally with Microsoft Excel for over a decade and has consulted on and taught Excel best practices to over 400 companies across 30+ countries.