Regex Find and Replace: 10 Practical Examples (Online, VS Code, Notepad++, Sheets)

· 6 minutes read

Regex Find and Replace: 10 Practical Examples (Online, VS Code, Notepad++, Sheets)

A normal find and replace changes one fixed word into another. A regex find and replace changes anything that matches a pattern: every date, every email address, every line that starts with a number. With capture groups you can even rearrange the parts of what you found.

This guide covers the few rules you need and ten patterns you can copy.

The fastest way: find and replace online

  1. Open Find and replace and paste your text.
  2. Set Mode to regular expression.
  3. Enter the pattern in Find and the replacement in Replace with.
  4. Check the replacement count, then copy the result.

You can add several rules – they run from top to bottom – and use \n for a line break or \t for a tab in the replacement. The text never leaves your browser.

The five rules you need

  • . matches any character, \d a digit, \w a letter, digit or underscore, \s whitespace.
  • + means "one or more", * "zero or more", ? "optional", {2,4} "two to four times".
  • ^ is the start of a line, $ the end of a line.
  • Parentheses ( ) create a capture group. In the replacement, $1 inserts what the first group matched, $2 the second, and amp; the whole match.
  • To match a special character literally, escape it: \. is a real dot, \( a real parenthesis.

10 practical examples

1. Swap first and last name

Find ^(\w+) (\w+)$, replace $2, $1.
"John Smith" → "Smith, John"

2. Reformat dates from ISO to European

Find (\d{4})-(\d{2})-(\d{2}), replace $3.$2.$1.
"2026-11-10" → "10.11.2026"

3. Add text to the start of every line

Find ^, replace -. Every line becomes a list item. (Or use the add prefix and suffix tool.)

4. Wrap every line in quotes and add a comma

Find ^(.*)$, replace "$1",.
apple → "apple",

5. Join lines into one comma-separated line

Find \n, replace ,.

6. Split a comma-separated list into lines

Find ,\s*, replace \n.

7. Remove everything in parentheses

Find \s*\([^)]*\), replace with nothing.
"Milk (2 l) and bread (wholegrain)" → "Milk and bread"

8. Collapse repeated spaces

Find {2,}, replace with one space. For a full cleanup, the trim text tool also handles tabs and non-breaking spaces.

9. Mask all but the last four digits

Find \d(?=\d{4}), replace *.
"4111111111111111" → "****1111" The (?=…) part is a lookahead: it checks what follows without replacing it.

10. Remove trailing whitespace from every line

Find [ \t]+$, replace with nothing.

How to use regex replace in other tools

VS Code – press Ctrl+H (Cmd+Option+F on Mac), click the .* icon or press Alt+R, and use $1 in the replacement. It works across files too (Ctrl+Shift+H).

Notepad++ – press Ctrl+H, choose Search Mode: Regular expression. Both \1 and $1 work in the replacement. Tick ". matches newline" only if you want . to cross lines.

Google Sheets – use REGEXREPLACE:

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

Sheets uses the RE2 engine, which does not support lookaheads like example 9.

Microsoft Word – Word's "Use wildcards" is not real regex. Groups are written ( ) and references \1, but quantifiers use {2,} and @ instead of +.

sed (Linux, macOS):

sed -E 's/^([A-Za-z]+) ([A-Za-z]+)$/\2, \1/' names.txt

sed uses \1 instead of $1, and \w is not available in the macOS version, so spell out character classes.

Common pitfalls

  • Greedy matching. \(.*\) on "a (b) c (d)" matches from the first "(" to the last ")". Use [^)]* or the lazy .*? instead.
  • Forgetting to escape. example.com also matches "exampleXcom". Write example\.com.
  • Line anchors. Whether ^ and $ match at every line depends on the "multiline" flag. Our tool enables it by default; in code you need the m flag.
  • Catastrophic backtracking. Nested quantifiers like (a+)+ or (\w+\s?)* can take exponential time on text that almost matches, freezing your editor. Simplify the pattern, for example to \w+(\s\w+)*.
  • Accented letters. In many engines \w matches only ASCII letters, so "Ján" is not one word. In JavaScript use \p{L}+ (with the u flag; our tool enables it automatically).

FAQ

What is the difference between $1 and \1?

Both mean "the first capture group". JavaScript, VS Code and Google Sheets use $1 in the replacement; sed, Word and many older tools use \1. Notepad++ accepts both.

How do I replace only the first match?

In our find and replace tool uncheck Replace all. In JavaScript, leave out the g flag.

Can I delete lines that contain a word?

A replacement like ^.*word.*\n does it, but the remove lines containing tool is simpler and also lets you keep only the matching lines.

Is it safe to paste confidential text?

Yes. The tool processes everything locally in your browser; nothing is uploaded.