Regex Replace Online — Find and Replace With Capture Groups

Home
Regex ToolkitAll tools
//
Flags:
Mode:
Replacement:
3 replacements
Original Text
Result65 chars
Your regex and test data stay in your browser.
3 lines · 65 bytes
Support project

Regex Replace Online — Find and Replace With Capture Groups

Write the pattern, write the replacement, and watch the result update line by line. Backreferences, named tokens and match counts are shown before you commit the change to a file, a migration, or an editor macro.

  • Use `$1`, `$2` or `${name}` in the replacement to reuse captured text.
  • Check first-or-all mode: without the `g` flag only the first match is replaced.
  • Escape literal dollar signs in the replacement as `$$`.

This tool runs entirely in your browser. Your patterns and text are not uploaded, stored, or logged.

Preview before you replace

The damage from a bad replace is proportional to how little you could see of the result. A live preview shows the exact output, including the whitespace and separators that a scrollback hides, so an over-greedy pattern is obvious before it rewrites a config file or a database dump.

Keep the input small while iterating. Paste two or three representative lines, get the pattern right, then paste the whole document. Reviewing the diff afterwards is a habit worth keeping even when the preview looked correct.

Capture groups instead of string surgery

Replacement templates are the reason regex beats repeated manual edits. Capturing the parts you want to keep and re-emitting them in a new order is declarative: the pattern states the shape of the input, the template states the shape of the output.

  • Reformat dates: `(?<year>\d{4})-(?<month>\d{2})` becomes `${month}/${year}`.
  • Normalize separators by capturing the value and re-emitting it with a fixed delimiter.
  • Wrap matches in markup or quotes without retyping the matched text.

Where regex replace goes wrong

Three mistakes cover most of it: an unanchored pattern that also matches inside longer tokens, a greedy quantifier that spans lines it should have stopped at, and a replacement that loses a group because the numbering shifted when the pattern changed. All three are visible in a preview and invisible in a dialog that only reports a count.

The replacement string is its own language

A JavaScript replacement string is not the same syntax as the pattern. $1 through $9 and $<name> pull in captured groups, $& is the whole match, and a literal dollar sign is written $$. A $ followed by a digit with no matching group is left as-is by the spec, but engines and editors differ, so confirm it in the preview rather than trusting it.

The replacement text is also not regex. Special characters in it are inserted literally: a backslash followed by n is two characters, not a newline, unless your language converts the escape before the replace call. This tool's replacement box is a single line, so it cannot insert a newline itself — do that in code, where the replacement string can hold a real line break.

Anchor it before you widen it

An unanchored pattern matches substrings, which is right for prose and dangerous for structured text. Line anchors and explicit lookarounds are what keep a replace inside the token you mean: ^ with the m flag pins a match to the start of each line, and a lookbehind or lookahead defines the characters that count as a boundary for your format. The word-boundary escape \b is convenient but ASCII-only — it treats $ and é as boundaries, so \bfoo\b still matches inside $foo or éfoo. Define the boundary explicitly when identifiers can contain those characters.

A rule of thumb: replace within a known boundary, not across the whole document. Run the preview on one line, then on a representative block, and only then on the full input — and keep the pre-change copy until the diff is reviewed.

Frequently asked questions

How do I reference a captured group in the replacement?

Use `$1`, `$2` and so on for numbered groups, or `${name}` for named groups. `$$` inserts a literal dollar sign, and `$&` inserts the whole match.

Why was only one occurrence replaced?

The pattern is missing the global flag. Without `g`, replace stops after the first match; switch the mode to replace all, or add the flag.

Is my text uploaded when I use this replace tool?

No. The pattern, the replacement string and the input text stay in your browser and are never sent anywhere or stored.

Why did my replacement insert a literal backslash-n instead of a newline?

The replacement box is a single line and treats text literally, so a backslash followed by n is two characters and you cannot type a newline. Do that transformation in code, where the replacement string can contain a real line break.

Related

Support the free tools