Find and Replace with Regex, Explained
After reading this you will know how a regular expression matches text, how capture groups feed the replacement, and how to predict the match count before you commit a single change.
Plain find and replace answers one question: where does this exact string appear? Regular expressions (regex) answer a broader one: where does this pattern appear? A pattern can say "a digit followed by two letters" or "a date in ISO order" without you typing every case by hand.
Here is the hook. Suppose you have 2024-03-15 and want 15/03/2024. You cannot list every date, so you search for the pattern (\d{4})-(\d{2})-(\d{2}) and replace with $3/$2/$1. The three parenthesised groups capture year, month and day, and the numbered references reorder them. One rule rewrites every date in the file.
What a regex actually is
A regular expression is a small program that reads your text left to right and tries to match characters against rules. This tool uses JavaScript regex syntax, so the rules are the ones the ECMAScript standard defines. The core pieces are short.
\d- Any single digit,
0through9. \w- A word character: a letter, a digit, or underscore. It does not include spaces or punctuation.
.- Any single character except a line break (unless the
sflag is set). {4}- A quantifier: exactly four of the thing before it.
{2,5}means two to five,+means one or more,*means zero or more. (…)- A capturing group. It matches what is inside and remembers the text so you can reuse it as
$1,$2and so on. (?:…)- A group that matches but does not capture. Use it for grouping without spending a number.
So (\d{4})-(\d{2})-(\d{2}) reads as: four digits (captured), a hyphen, two digits (captured), a hyphen, two digits (captured). Against 2024-03-15 it matches all ten characters and stores 2024, 03 and 15 in groups 1, 2 and 3.
When to reach for regex, and when not to
Plain matching is the right choice when the text you want is fixed and literal. Replacing every color with colour needs no pattern. Plain mode is also safer, because characters like . and ( mean themselves rather than something clever.
Regex earns its keep when the target varies but follows a shape. Phone numbers, dates, log timestamps, HTML tags, trailing whitespace, repeated blank lines: all of these are patterns rather than fixed strings. If you catch yourself running the same plain replacement fifteen times with slightly different inputs, one regex probably covers all fifteen.
Regex is a poor fit for deeply nested structures. HTML and JSON can nest without limit, and a regular expression cannot count nesting depth. A pattern like <.*> will happily match across several tags at once. For structured data, use a real parser. Regex is fine for flat, local patterns inside such text.
How the match engine scans
The engine starts at position 0 and tries the pattern. If it fails, it advances one position and tries again. When a match succeeds, the engine records it, then continues from the character after the match. This "leftmost, then advance" rule explains most surprises.
Two quantifier behaviours matter. By default quantifiers are greedy: they take as much as they can while still allowing the overall pattern to match. Add a ? to make them lazy, taking as little as possible.
Reading this: the greedy .* in <.*> swallows everything up to the last >, so the single match is the whole string <a><b>, all 6 characters. Change it to the lazy <.*?> and you get two matches, <a> and <b>, 3 characters each. Same input, different quantifier, different count.
Flags that change the whole match
Flags are switches that apply to the entire pattern. Three do most of the work.
| Flag | Name | Effect |
|---|---|---|
i | ignore case | Cat matches cat, CAT and cAt. |
m | multiline | ^ and $ match at each line break, not only at the very start and end. |
s | dotall | . also matches line breaks, so a pattern can span lines. |
Counting example: the text a\nA\na (three lines: a, A, a) searched for a gives 2 matches, but searched for a with the i flag gives 3. The flag adds exactly the one uppercase line.
A worked example you can reproduce
Load the tool's demo data and follow along. The sample text is:
2024-03-15 login ok
2024-03-16 login fail
2024-03-17 login ok
The demo searches for (\d{4})-(\d{2})-(\d{2}) and replaces with $3/$2/$1.
Reordering three dates
- The engine starts at position 0 of line 1. It reads
2024into group 1, the hyphen,03into group 2, the hyphen,15into group 3. Match found:2024-03-15, 10 characters. - It builds the replacement by substituting each reference:
$3becomes15,$2becomes03,$1becomes2024. Result:15/03/2024. - The engine continues from the character after the match. The rest of line 1 (
login ok) contains no digit run of the right shape, so no second match on that line. - Lines 2 and 3 match the same way, producing
16/03/2024and17/03/2024.
The counter shows 3 replacements. Each of the three lines contributes exactly one match, because each holds exactly one date in that shape. The words login, ok and fail never match, since they contain no digits.
Watch quantifiers change the count
The single idea worth playing with is how greedy versus lazy quantifiers and the i flag change how many matches you get on the same input. Move the controls and watch the count and the highlights.
Reading the preview and the counter
Before you commit, the tool highlights every match and shows a count. Read both. The count is your sanity check: if you expected 3 and see 47, your pattern is too broad. If you expected 3 and see 0, it is too strict or has a typo.
The highlight tells you the extent of each match, which the count alone hides. A common shock is one giant highlight where you expected several small ones. That is greed at work. The fix is usually a lazy quantifier or a more specific character class such as [^>]* instead of .*.
Test your pattern on a small slice first. Paste five representative lines, confirm the count matches your hand count, then paste the full file. A pattern that is right on 5 lines is almost always right on 5000.
Common mistakes
These are the errors that produce wrong counts most often.
- Forgetting to escape special characters. In regex mode,
.matches any character and3.14matches3x14too. To match a literal dot, write\.or switch to plain mode. - Greedy
.*across a whole line. As shown above, it eats more than you want. Prefer.*?or a negated class. - Using
$1where no group exists. If your pattern has no parentheses,$1inserts the literal text$1, not a captured value. - Wanting a literal dollar sign. Write
$$in the replacement to get one$. A lone$followed by a digit is read as a group reference. - Expecting
^per line without themflag. Withoutm,^matches only at the very start of the whole text.
Related tools
Regex handles patterns, but some jobs have a dedicated tool that is faster and harder to get wrong.
To change letter case rather than content, use the Case Converter. To remove duplicate lines, sort or number them, use Line Tools: Dedupe, Sort & Shuffle. To cut, pad or hard-wrap lines to a fixed width, use Truncate, Pad & Wrap. To turn café into cafe without writing a character class for every accent, use the Accent & Diacritic Remover. To reverse or flip text, use Reverse & Upside-down Text.
Frequently asked questions
Why does my replacement show $1 instead of the captured text?
Your pattern has no capturing group, so there is nothing for $1 to reference and it prints literally. Wrap the part you want to reuse in parentheses. For example, change \d{4} to (\d{4}) to make $1 work.
How do I match a literal parenthesis or dot?
Escape it with a backslash: \(, \), \.. Alternatively switch to plain matching, where every character means itself.
Does the tool send my text to a server?
No. Everything runs in your browser, so nothing you paste leaves your device. That also means the speed depends on your machine, not a network connection.
Why does one match cover more text than I expected?
A greedy quantifier such as .* extends as far as it can. Make it lazy with .*?, or replace . with a class that excludes your delimiter, such as [^>] when matching inside tags.
Can I match across line breaks?
Add the s flag so that . also matches newlines. Without it, . stops at each line break, and a pattern spanning two lines will not match.