Find and Replace
- 2
- Replacements
- 61
- Characters in
- 61
- Characters out
The dog sat on the mat. The catalogue mentions the dog twice.
Find and replace swaps one string for another across a block of text, with three options that decide what counts as a match: case sensitivity, whole words only, and treating the search term as a regular expression. The replacement count is shown, which is the quickest way to confirm the match did what you expected before copying.
How it works
- Plain mode escapes every regular-expression character in your search term, so a full stop matches a full stop rather than any character.
- Whole words wraps the term in word boundaries, so "cat" matches the animal but not "catalogue".
- Regex mode passes your pattern through unescaped, with capture groups available in the replacement as $1, $2 and so on.
- Matching is global. Every occurrence is replaced, not only the first.
Whole words and regex are mutually exclusive: adding word boundaries to an arbitrary pattern produces something that rarely matches what the author intended, so selecting one turns off the other.
Examples
Whole words only
Text
The cat sat. The catalogue mentions the cat.
Find
cat
Whole words
On
Result
Two replacements: "catalogue" is untouched
Without the whole-word option this makes three replacements and turns catalogue into dogalogue.
A regular expression with a capture group
Find
(\\w+)@(\\w+)\\.com
Replace
$1 at $2
Regex
On
Result
sales@example.com becomes "sales at example"
Capture groups are referenced as $1 and $2 in the replacement, which is what makes regex mode worth the extra care.
An invalid pattern
Find
(unclosed
Regex
On
Result
The error is reported and the text is left unchanged
A malformed pattern is caught rather than silently matching nothing, which is how a bad replace goes unnoticed.
Frequently asked questions
Why can I not use whole words and regex together?
Because word boundaries around an arbitrary pattern almost never mean what the author intended, wrapping \d+ in boundaries changes which numbers match in ways that surprise people. If you need boundaries in a pattern, write \b yourself in regex mode.
Do I need to escape special characters in plain mode?
No. Plain mode escapes them for you, so searching for "3.14" finds that exact string rather than any character between 3 and 14. Only regex mode treats your input as a pattern.
How do I use a captured group in the replacement?
With $1, $2 and so on, numbered by the opening bracket. $& inserts the whole match. This works only in regex mode; in plain mode a dollar sign is a literal dollar sign.
Does it replace every occurrence or just the first?
Every one. The global flag is always set. There is no single-replacement mode, because the far more common mistake is replacing only the first occurrence without noticing.