Regex Find & Replace: A Practical Guide for Writers and Developers
Standard find-and-replace only gets you so far. The moment you need to match patterns — "all phone numbers", "every word in CAPS", "duplicate spaces" — you need regular expressions (regex).
This guide walks you through the core concepts with practical examples you can paste directly into the Find & Replace tool. No prior programming experience needed. Every example includes exactly what to type in the Find and Replace fields.
What Is a Regular Expression?
A regular expression is a mini-language for describing text patterns. Instead of searching for the exact string cat, you can describe patterns like "any word ending in -tion" or "any sequence of digits."
Regex was invented by mathematician Stephen Kleene in the 1950s and has been part of every major text editor and programming language since the 1980s. The syntax is standardised enough that patterns written for this tool will also work in VS Code, Python, Notepad++, and most other editors — with minor variations.
The Three Toggle Options Explained
Before diving into patterns, understand the three flags available in the Find & Replace tool:
- Use Regex — turns on regex mode. Without it, special characters like
.and*are matched literally. Enable this whenever you use any pattern below. - Case Sensitive — when off (default),
hellomatchesHello,HELLO, andhElLo. Turn it on when you need exact-case matching — for example, when replacing a variable name that shares letters with a common word. - Global Match — when on (default), every match in the text is replaced. When off, only the first match is replaced. Use "off" when testing a new pattern to preview what it matches before committing to a full-document replacement.
The Core Building Blocks
| Character | Meaning | Example |
|---|---|---|
. | Any single character (except newline) | c.t matches "cat", "cut", "c3t" |
* | Zero or more of the preceding | ab*c matches "ac", "abc", "abbc" |
+ | One or more of the preceding | ab+c matches "abc", "abbc" but not "ac" |
? | Zero or one of the preceding | colou?r matches "color" and "colour" |
\d | Any digit (0–9) | \d+ matches "42", "2026" |
\w | Any word character (letters, digits, _) | \w+ matches "hello", "word_1" |
\s | Any whitespace (space, tab, newline) | \s+ matches spaces and tabs |
^ | Start of line | ^Hello matches "Hello" at line start only |
$ | End of line | end$ matches "end" at line end only |
\b | Word boundary | \bcat\b matches "cat" but not "catfish" |
( ) | Capture group | (\d+) captures digits for reuse as $1 |
[ ] | Character class | [aeiou] matches any vowel |
| | Or | cat|dog matches "cat" or "dog" |
Essential Patterns with Step-by-Step Examples
1. Match Any Number: \d+
Find: \d+Replace: [NUMBER]Input: "Call 555-1234 or 800-9876"
Output: "Call NUMBER-NUMBER or NUMBER-NUMBER"
Use this to anonymise numeric data in a document before sharing it.
2. Collapse Extra Whitespace
Find: {2,} (two or more spaces)
Replace: (one space)
This cleans up pasted text from PDFs, emails, or other sources that insert double-spaces between words. For mixed whitespace including tabs, use \s+ with a single space replacement. Or skip the regex entirely and use the Fix Spaces tool — it handles this in one click.
3. Match Whole Words Only: \b
Find: \bcat\bReplace: dog
Matches "cat" but not "catfish", "tomcat", or "bobcat". Word boundaries (\b) are essential when replacing short words that appear as substrings in longer words. Classic junior mistake: replacing the without boundaries and mangling words like "these" and "other".
4. Reformat Dates with Capture Groups
Find: (\d{4})-(\d{2})-(\d{2})Replace: $3/$2/$1Input: "2026-03-14"
Output: "14/03/2026"
Parentheses create capture groups. The matched content of the first group becomes $1, second becomes $2, and so on. You can reorder, duplicate, or wrap captured content in the replacement string.
5. Strip Leading Whitespace from Every Line
Find: ^[ \t]+Replace: (empty)
Removes all spaces and tabs at the start of each line. Useful when pasting code or formatted lists that picked up indentation from the source.
6. Normalise Spelling Variants
Find: colou?rReplace: color
The ? makes the "u" optional, matching both "color" and "colour" in one pass. For more distinct variants: organise|organize.
7. Remove Blank Lines
Find: ^\s*$\nReplace: (empty)
Matches lines that contain only whitespace (including completely empty lines) and removes them. Halves the visual height of heavily double-spaced documents.
8. Extract or Replace Email Addresses
Find: [\w.+-]+@[\w-]+\.[\w.]+Replace: [email redacted]
Matches the vast majority of standard email addresses. Use it to scrub contact data from documents before distributing them externally.
Use Cases by Profession
Writers and Editors
- Replace all em dashes with en dashes: Find
—, replace with–(or vice versa, depending on your style guide) - Find double spaces after periods: Find
\.(period + two spaces), replace with.(period + one space) — eliminates the legacy typewriter habit in one pass - Replace straight quotes with curly quotes: Find
", replace with"or"depending on position (requires two passes — opening and closing) - Flag sentences starting with a lowercase letter: Find
\. [a-z]to identify them for manual review
Developers and Data Workers
- Strip HTML tags: Find
<[^>]+>, replace with nothing — extracts plain text from HTML source - Normalise line endings: Find
\r\n, replace with\n— converts Windows CRLF to Unix LF - Reformat name lists: Find
^(\w+)\s(\w+)$, replace with$2, $1— converts "John Smith" → "Smith, John" for sorting - Remove single-line comments: Find
\/\/.*$— matches JavaScript comments through end of line - Mask credit card numbers: Find
\b(\d{4})[- ]?(\d{4})[- ]?(\d{4})[- ]?(\d{4})\b, replace with****-****-****-$4
Ready-to-Use Pattern Libraries
For Writers
| Task | Find | Replace with |
|---|---|---|
| Remove double spaces | {2,} | (one space) |
| Remove trailing spaces per line | [ \t]+$ | (empty) |
| Find sentences missing end punctuation | [a-z]\n | Review manually |
| Replace smart apostrophes | ['''] | ' |
| Convert all-caps words | \b[A-Z]{2,}\b | Review manually |
| Find repeated words | \b(\w+)\s+\1\b | $1 |
For Developers
| Task | Find | Replace with |
|---|---|---|
| Strip HTML tags | <[^>]+> | (empty) |
| Redact emails | [\w.+-]+@[\w-]+\.[\w.]+ | [email] |
| Mask phone numbers | \b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b | [phone] |
| Convert Windows to Unix line endings | \r\n | \n |
| Remove single-line comments | \/\/[^\n]* | (empty) |
| Collapse 3+ blank lines to 2 | \n{3,} | \n\n |
| Trim trailing whitespace | \s+$ | (empty) |
Tips for Writing Regex Patterns
- Start simple. Begin with a literal string and add one special character at a time. Confirm each addition matches what you expect before continuing.
- Test with Global Match off first. See what the first match looks like before replacing all occurrences. One unexpected match is easier to investigate than a document full of wrong replacements.
- Escape literal special characters. To match an actual dot, use
\.not.(which matches any character). Same for\$,\(,\),\[. - Use character classes for flexibility.
[aeiou]matches any vowel.[^aeiou]matches any non-vowel.[A-Z]matches any uppercase letter. - Be specific about what you don't want to match. If your pattern is too broad, add anchors (
^,$) or word boundaries (\b) to restrict it.
Common Mistakes to Avoid
- Using
.*without thinking. The.*pattern is greedy — it matches as much as possible.<.*>applied to<b>hello</b>matches the entire string, not just one tag. Use<[^>]+>instead. - Forgetting to escape backslashes. In some contexts,
\dmust be written as\\d. In the Find & Replace tool, a single backslash works as expected. - Replacing without a backup. Always keep a copy of your original text before running a global replacement on an important document.
Understanding Greedy vs Lazy Matching
One of the most common sources of regex bugs. By default, + and * are greedy — they match as much as possible. The pattern <.+> applied to <b>bold</b> matches the entire string <b>bold</b>, not just <b>, because .+ expands to consume as many characters as it can while still allowing the rest of the pattern to match.
Add a ? after a quantifier to make it lazy (match as little as possible): <.+?> matches only <b>. This distinction matters whenever you're matching paired delimiters like HTML tags, quotation marks, or parentheses.
How to Debug a Pattern That Isn't Working
Work through this checklist when a pattern returns no matches:
- Confirm Regex mode is enabled — without it, special characters are treated as literal text
- Check for unescaped special characters — a bare
(or[without a closing counterpart causes a parse error - Check case sensitivity — if your text uses "Hello" and your pattern uses "hello" with Case Sensitive on, there will be no match
- Test with a shorter version of your pattern to isolate which part is failing — remove quantifiers, character classes, and groups one at a time until you find the element that breaks the match
Try It Now
Open the Find & Replace tool, paste your text, enable Regex, and start experimenting with the patterns above. All processing happens in your browser — your text never leaves your device, and there's no limit on document length or number of replacements.