0Pricing
Excel Formulas Academy · Lesson

Replacing Patterns With REGEXREPLACE

Swap matched text for new values across a cell.

Replacing Patterns With REGEXREPLACE is a free Excel Formulas Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Excel Formulas Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Rewriting Text by Pattern

So far you tested patterns and pulled pieces out. Now you will rewrite text: stripping junk characters, reformatting phone numbers, or collapsing extra spaces.

REGEXREPLACE finds every part of the text that matches a pattern and swaps it for replacement text you choose.

This Google Sheets function is the heavy lifter of text cleaning, because it can transform a whole messy column in one pass.

The REGEXREPLACE Syntax

REGEXREPLACE(text, pattern, replacement) takes three arguments: the source text, the pattern to find, and the replacement string.

Unlike REGEXEXTRACT, it replaces every match it finds, not just the first.

The formula below removes all digits from A2 by replacing each digit with an empty string. Room 204 becomes Room .

=REGEXREPLACE(A2, "\d", "")

Deleting by Replacing With Nothing

A common trick is to delete characters by replacing them with an empty string "".

The formula below strips out anything that is not a digit, leaving only the numbers. (555) 123-4567 becomes 5551234567. The [^\d] class means any character that is not a digit, and + grabs runs of them at once.

=REGEXREPLACE(A2, "[^\d]+", "")

Worked Example: Collapse Extra Spaces

Imported text often has double or triple spaces. You want to squeeze any run of spaces down to a single space.

The formula below matches one or more whitespace characters and replaces each run with a single space. Combine it with TRIM later to clean the ends too.

=REGEXREPLACE(A2, "\s+", " ")

Backreferences Reuse the Match

The replacement text can refer back to captured groups using $1, $2, and so on. $1 means whatever the first capture group matched.

This lets you reorder or reformat. The formula below turns Smith, John into John Smith by capturing the two names and swapping their order.

=REGEXREPLACE(A2, "(\w+), (\w+)", "$2 $1")

Worked Example: Format a Phone Number

Suppose A2 holds a clean 10-digit string like 5551234567 and you want (555) 123-4567.

Capture three digits, three digits, and four digits, then rebuild with formatting in the replacement. Each group is referenced by its number.

=REGEXREPLACE(A2, "(\d{3})(\d{3})(\d{4})", "($1) $2-$3")

Replacing Specific Words

You can replace literal words too. To standardize abbreviations, match the word and swap it.

The formula below changes every St. into Street in an address. Remember to escape the dot as \. so it matches a literal period rather than any character.

=REGEXREPLACE(A2, "St\.", "Street")

Case Insensitive Replacement

Regex is case sensitive by default. To match a word regardless of case, list the variants in a class or build alternatives.

The formula below replaces color or Color with colour. The [Cc] matches either case of the first letter.

=REGEXREPLACE(A2, "[Cc]olor", "colour")

Worked Example: Mask Sensitive Digits

To hide all but the last four digits of a card number, replace the leading digits with X.

The formula below matches all digits that come before the final four and replaces each with an X. For 1234567890123456 you get XXXXXXXXXXXX3456.

=REGEXREPLACE(A2, "\d(?=\d{4})", "X")

Chaining Cleanups Together

Real cleaning often needs several steps. You can nest REGEXREPLACE calls, with the inner result feeding the outer.

The formula below first removes anything that is not a letter or space, then collapses multiple spaces into one. Reading inside out shows the order of operations.

=REGEXREPLACE(REGEXREPLACE(A2, "[^A-Za-z ]", ""), "\s+", " ")

Applying It Down a Whole Column

Like the other regex functions, REGEXREPLACE is meant to clean an entire column in one pass. You write the formula once and fill it down beside your raw data.

Suppose column A holds product names cluttered with trailing codes like Chair (SKU-4821). The formula below removes the parenthetical code from every row, leaving just Chair . Add a TRIM around it to drop the leftover space.

=TRIM(REGEXREPLACE(A2, "\(.*\)", ""))

Quick Check

Test your grasp of pattern replacement.

Recap: Transform Text in One Pass

You learned that REGEXREPLACE(text, pattern, replacement) rewrites every match in Google Sheets. Key points:

  • Replace with "" to delete unwanted characters
  • [^\d] and \s+ are workhorses for cleaning
  • Capture groups can be reused in the replacement as $1, $2
  • Escape literal specials like \.
  • Nest calls to chain multiple cleanups

Next you will pair regex with SPLIT and CLEAN to break apart and finish tidying imported data.

Frequently asked questions

Is the “Replacing Patterns With REGEXREPLACE” lesson free?

Yes — the full text of “Replacing Patterns With REGEXREPLACE” is free to read here on the web, and the Excel Formulas Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Excel Formulas Academy course, upgrade to CoddyKit PRO.

What will I learn in “Replacing Patterns With REGEXREPLACE”?

Swap matched text for new values across a cell. You practise Excel Formulas Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Excel Formulas Academy?

No prior experience is required. Excel Formulas Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Replacing Patterns With REGEXREPLACE” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Excel Formulas Academy lesson?

Yes. Every Excel Formulas Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Testing Patterns With REGEXMATCH
  2. Pulling Out Patterns With REGEXEXTRACT
  3. Replacing Patterns With REGEXREPLACE
  4. Splitting and Cleaning Messy Data
← Back to Excel Formulas Academy