Back to Developer
Text replace — regex with backreferences ($1)

Text replace — regex with backreferences ($1)

Replace substrings across a body of text. Plain strings or regex (ignore-case, multiline (^$ per line), dot matches newline) with backreferences ($1 $2 / $<name>). Newlines accepted via \n in the replacement, and the substitution count is reported. Runs entirely in your browser.

developertext

How to use

Enter the search pattern, replacement, and target text. The replaced output appears below. With Regex off, special characters like `/` and `.` are treated literally (plain replace). With Regex on, the pattern is parsed as a JavaScript regular expression and you can use backreferences (`$1` / `$2` / `$<name>`), groups, character classes, and more. Ignore case, Multiline (`^$` per line), Dot matches newline, and Replace all toggle independently. Use `\n` in the replacement to insert a newline. The number of replacements is shown in the status line. Use Sample to try a worked example. If the regex is invalid, an error appears with a CTA link to regex-test.

FAQ

Is text uploaded?
No. Replacement uses `String.replace` / `RegExp` entirely in the browser; no input data is transmitted.
Which regex dialect?
JavaScript (ECMAScript) regex. Constructs from Perl / PCRE / Python re that aren't standard JS (e.g. `\K`, recursion, `(?P<name>)`) won't work. Angle-bracket named captures `(?<name>)` work in ES2018+.
How do I write backreferences?
In the replacement, use `$1` `$2` … (numbered capture groups) or `$<name>` (named captures). To emit a literal `$`, write `$$`.
How do I insert a newline in the replacement?
Type `\n` (backslash + n) into the replacement field, or paste an actual newline. The tool expands `\n` `\t` `\r` into the real control characters at replacement time (write `\\n` to keep a literal backslash-n).
What does turning off Replace all do?
In regex mode the `g` flag is dropped, so only the first match is replaced. In plain-string mode, this corresponds to `String.replace`, replacing only the first occurrence.
The regex is reported invalid
Unbalanced `(` / `[`, invalid backreferences, or non-ES constructs cause the RegExp constructor to throw. The error message and a CTA link to regex-test let you iterate on the pattern there.
How is this different from regex-test?
regex-test focuses on showing what a pattern matches. This tool focuses on producing the replaced output. Pair it with regex-explain to safely build complex replacements.

Related tools