Text Toolkit

September 24, 2026

Regex find and replace: a practical cheat sheet

Plain find and replace swaps one exact piece of text for another. Regular expressions (regex) let you match patterns instead: any number, any email address, any line that starts with a certain word. Combined with replacement references, they can restructure text in ways that would take hours by hand. This is a practical cheat sheet for the patterns that come up most in everyday text cleanup.

The building blocks

  • . matches any single character except a line break.
  • \d matches a digit, \w a letter, digit, or underscore, and \s any whitespace, including spaces, tabs, and line breaks.
  • [abc] matches any one of the listed characters; [a-z] matches a range; [^abc] matches anything except those characters.
  • ^ matches the start of a line and $ the end of a line. (Some tools apply these to the whole text instead unless multiline mode is on; the tool on this site always works line by line.)
  • \ escapes a special character, so \. matches a literal period.

Repetition

  • * means zero or more, + means one or more, and ? means zero or one.
  • {3} means exactly three, and {2,5} means between two and five.
  • Adding ? after a repeat makes it lazy, matching as little as possible: ".*?" matches one quoted string rather than everything from the first quote to the last.

Groups and references

Parentheses create a capture group. In the replacement, $1 refers to the first group, $2 to the second, and $& to the whole match. This is what makes regex replace so powerful.

Recipes

  1. Swap "Last, First" to "First Last": find (\w+), (\w+) and replace with $2 $1.
  2. Collapse repeated spaces: find {2,} (a space followed by a count) and replace with a single space.
  3. Remove trailing whitespace: find [ \t]+$ and replace with nothing.
  4. Wrap every number in brackets: find \d+ and replace with [$&].
  5. Reformat dates from 2026-09-24 to 09/24/2026: find (\d{4})-(\d{2})-(\d{2}) and replace with $2/$3/$1.
  6. Convert tabs to commas: find \t and replace with ,.
  7. Strip HTML tags from a snippet: find <[^>]+> and replace with nothing. (Fine for quick cleanup, but not a safe way to sanitize untrusted HTML.)
  8. Find duplicated words like "the the": find \b(\w+) \1\b and replace with $1.

Common mistakes

  • Forgetting to escape special characters. Characters like . * + ? ( ) [ ] { } | ^ $ \ all have special meaning. To match a literal period in "example.com", use example\.com, or turn regex mode off so the text is matched literally.
  • Greedy matches that swallow too much. <.*> on a line with two tags matches from the first < to the last >. Use <.*?> or <[^>]*> instead.
  • Case sensitivity. Decide whether "Apple" and "apple" should both match, and set the option accordingly.
  • Testing on the whole document at once. Try a pattern on a small sample first, and check the match count before trusting the result.

Try it

The find and replace tool supports JavaScript regular expressions with $1 and $& references in regex mode, shows a live match count, and never changes your original text, so you can experiment safely. With regex mode off, both the search and replacement text are treated literally.

Want to try it yourself?

Open the Find and Replace →

Share this guide