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

ArgumentRequired?What it does
textYesThe original text.
patternYesThe regular expression to find.
replacementYesWhat to put in. "" removes the match.
occurrenceOptional0 = every match (default), 2 = the second, -1 = the last, and so on.
case_sensitivityOptional0 = 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")
AB
1NoteMasked
2Call 604-555-0199Call ***-***-0199
3Mobile: 403-555-0142Mobile: ***-***-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]", "")
AB
1Typed phoneDigits
2(604) 555-01996045550199
3403.555.01424035550142
=REGEXREPLACE(A2, "[^0-9]", "") Explain

Common mistakes

  • Special characters need a backslash. To match a real dot, bracket or plus sign, write \., \( or \+.

Notes

Kyle Stott

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.