Mastering Find and Replace (With Regex Power)
How plain-text, whole-word and regex find-and-replace actually behave, tested on our own tool: whole-word matching missed "café" and every Cyrillic word, and a "$$" in a plain-text replacement turned into one dollar sign, until we fixed both. Plus replacement patterns, pipelines and why a regex can change meaning between languages.
Three Modes, Three Different Searches
- Plain text finds the characters exactly as typed, including inside longer words, and inserts the replacement exactly as typed.
- Whole word finds the text only where it is not part of a longer word, so "cat" skips "category" and "concatenate".
- Regex treats the search as a JavaScript regular expression, with character classes (
\d,\w,\s), quantifiers, anchors and capture groups, and reads$1-style references in the replacement.
What We Found When We Tested Our Own Tool
We ran the Find and Replace tool's own matching code on eight cases with known answers. Half failed, so we fixed the tool and ran them again:
| Case | Before the fix | After |
|---|---|---|
| Whole word: cat | correct | correct |
| Whole word: accented | café bars café. | correct |
| Whole word: Cyrillic | кот котик кот | correct |
| Plain text: $$ in replacement | US$9 | correct |
| Plain text: $& in replacement | xx | correct |
| Regex: swap names | correct | correct |
| Regex: reformat date | correct | correct |
| Pipeline: collapse then trim | correct | correct |
Two separate bugs were behind the failures. Whole-word mode wrapped the search in \b, and in JavaScript \b only knows ASCII letters, so an accented "é" counted as a word boundary: "café" matched inside "cafés" and never on its own, and Cyrillic words never matched at all. Whole-word mode now checks for letters and digits in any script with Unicode property escapes (\p{L}, \p{N}) in lookbehind and lookahead, which MDN's compatibility data lists in Chrome 62, Firefox 78 and Safari 16.4. Second, plain-text replacements went through JavaScript's replacement patterns, so $$ collapsed to one dollar sign and $& inserted the match; plain-text mode now inserts the replacement literally.
The same pattern can behave differently elsewhere. Python's \b is Unicode-aware for text, so re.sub(r'\bcafé\b', 'bar', 'café cafés café.') gives the right answer that JavaScript's \b does not. Davis and colleagues (2019, found through alphaXiv) surveyed 158 developers, most of whom reuse regexes across languages, and tested 537,806 regexes from 193,524 projects in eight languages: 15% behaved differently in another language and 10% performed differently. Test a pasted regex in the engine you will run it in.
Replacement Patterns in Regex Mode
| In the replacement | Inserts |
|---|---|
$1, $2 | the text matched by capture group 1, 2... |
$<name> | a named group, (?<year>\d{4}) |
$& | the whole match |
$$ | a literal dollar sign |
Swapping "Smith, John" to "John Smith" with (\w+), (\w+) and $2 $1, and turning 2026-04-15 into 04/15/2026 with (\d{4})-(\d{2})-(\d{2}) and $2/$3/$1, both passed. A replacement string cannot change case; there is no lowercase or uppercase operator in JavaScript replacements, so normalizing capitalization needs a separate step outside the tool.
Multi-Rule Pipelines
Rules run top to bottom, each on the previous rule's output. Collapsing whitespace with \s+ to one space and then trimming with ^\s+|\s+$ turned " a b " into "a b" in our test. One limit to know: the tool does not set the multiline flag, so ^ and $ match the start and end of the whole text, not of each line. To act on every line, match the line break itself, for example \n\s+ to strip leading spaces after each newline.
Real-World Workflows
- Moving to HTTPS: one plain-text rule,
http://example.comtohttps://example.com. Include the host so other sites' links are left alone. - Renaming a variable: whole-word mode, so
oldNamechanges butoldNameExtradoes not. - Masking ID numbers in logs: regex
\b\d{3}-\d{2}-\d{4}\btoXXX-XX-XXXX; the\banchors stop it from cutting into longer digit runs. Everything runs in your browser, so the log never leaves your device. - Stripping Markdown: remove
**before*, then turn links\[([^\]]+)\]\([^)]+\)into$1. Order matters, which is what pipelines are for.
Try the tool
Plain text or regex, multi-rule sequential replacement.
Frequently Asked Questions
Why is my regex not matching?
What's the difference between whole-word and plain text?
Can I use capture groups with plain text?
How do multi-rule pipelines work?
Is this safe for sensitive data?
Published Apr 15, 2026 · Updated Oct 14, 2026 by Derek Giordano
- [1]MDN, String.prototype.replace(): specifying a string as the replacement ($$, $&, $n, $<name>) ↗
- [2]MDN, Unicode character class escape (\p{...}) ↗
- [3]MDN, Lookbehind assertion (compatibility checked live Oct 14, 2026) ↗
- [4]Python documentation, re module: \b and Unicode word characters ↗
- [5]Davis JC, Michael LG, Coghlan CA, Servant F, Lee D (2019), Why Aren't Regular Expressions a Lingua Franca? An Empirical Study on the Re-use and Portability of Regular Expressions, ESEC/FSE 2019; arXiv:2105.04397 (via alphaXiv) ↗