Text 🔒 Runs in your browser — no data sent to servers

Regex Find and Replace

Rewrite text with a regular expression and see the result before you run it on real files.

/ /
Test String
Matches & Groups
Matches appear here...

Regex Find and Replace Online

Search and replace with a regular expression is one of the fastest ways to clean up text, and one of the easiest to get wrong on a thousand lines at once. This page opens the Regex Tester with a working example: the Repeated word pattern from the Pattern library, \b(\w+)\s+\1\b, and the replacement $1. The sample sentence comes back under Replace Result with every doubled word removed.

How the example works

(\w+) captures a word, \s+ matches the space or line break after it, and \1 asks for the same word again. The \b at each end stops it from matching inside longer words, so "the theory" is left alone. With the i flag, "This this" counts as a repeat, and $1 keeps the first spelling: "This this is a a typo" becomes "This is a typo".

Replacement tokens

TokenInserts
$1, $2The text of that numbered group
$<name>The text of a named group, such as (?<year>\d{4})
$&The whole match
$`Everything before the match
$'Everything after the match
$$A literal dollar sign

Test before you replace in bulk

Most replace disasters come from a pattern that matches more than you meant. Paste a representative sample, including lines that must not change, and read the highlighted matches before trusting the result. Two traps are common: a greedy .* runs to the last possible match on the line, and without the g flag only the first match is replaced.

Words in other scripts

In JavaScript, \w and \b only know the letters A to Z, the digits and the underscore, so the example misses a repeated "café" or a repeated Persian word. With the flags giu, the pattern (?<![\p{L}\p{N}_])([\p{L}\p{N}_]+)\s+\1(?![\p{L}\p{N}_]) does the same job in any alphabet.

Related pages

The JavaScript regex tester explains the flags and the engine behind this page, and the URL regex is a good pattern to replace with, for example to turn bare links into Markdown with [$&]($&). The full tool with all 15 ready-made patterns is the Regex Tester.

Guides