Regex Replace Tool

Test regular expressions with real-time match highlighting and apply find-and-replace substitutions. Supports capture group back-references.

//g
Replace with:
Flags:global ·
Test String0 chars

When a regex replace tool is the right tool

Use this page to try a JavaScript regular expression against a test string, see the matches highlighted, and preview a replacement that can use $1 or other JS backreferences. Everything updates as you type. It is not a PCRE or Python engine, and it is not a file-wide find-and-replace. For match testing without a replacement field, use Regex Tester.

Use cases

Wrapping or rewriting captures

Type a pattern such as (\w+), a replacement such as [$1], and a short test string. With g on, every word is wrapped. Turn g off and only the first match is replaced, even though the Matches panel still lists every match (that list always searches globally).

Using flags

Toggle i for case-insensitive matching, m so ^ and $ follow line breaks, s so . matches newlines, and u for Unicode mode. The letters after the trailing slash show the current flag string.

Reading named groups

If the pattern uses (?<year>\d{4}), Match Details can show year: "2020". Numbered groups still participate in $1 replacements but are not printed in that list.

How matching and replacement work

A RegExp is built from the pattern and flags. Matches are collected with exec in a loop that forces g. Highlighting is an HTML-escaped copy of the test string with each match wrapped in a mark. Replacement uses the flags you selected, so it follows the engine’s first-match-only rule when g is off. An empty pattern leaves the test string unchanged and does not run a replace.

Example — a global word wrap with $1
Pattern:      (\w+)
Replacement:  [$1]
Flags:        g
Test string:  hello world

Matches:      hello (index 0), world (index 6)
Replace:      [hello] [world]

Without the g flag, only the first word is replaced: [hello] world

The placeholder under Replace with reminds you that $1 and $2 are available. A literal dollar sign in the output is $$.

What this tool does not do

It does not run PCRE, pass over files on disk, or offer a sticky y flag. A replacement that clears the string can look like the empty-output placeholder. Nothing is uploaded.

How to use Regex Replace

  1. Enter a pattern, optional replacement, and flags (g starts on).
  2. Paste a test string. Matches, details, and the replace preview appear when that box is not empty.
  3. Copy Matches or Copy Result when you want the text on the clipboard.

Frequently asked questions

Which regular expression syntax and flags are supported?

This is JavaScript RegExp in your browser, not PCRE or Python. The flag buttons are g, i, m, s, and u (global, case-insensitive, multiline, dotAll, unicode). There is no y (sticky), d, or v flag. g is on by default. Invalid patterns show the engine’s error message under the pattern box.

How do replacements and capture groups work?

The replace result is testString.replace(regexp, replacement), so JavaScript backreferences apply: $1, $2, $&, $`, $', and $$ for a literal dollar, and $<name> for a named group in engines that support it. The placeholder under the replacement field mentions $1 and $2. If g is off, only the first match is replaced. Match listing and highlighting always search globally (the tool adds g for those passes even when you have turned it off).

What do the Matches and Match Details panels show?

Matches highlights every global match in the test string (the text is HTML-escaped, then wrapped in a mark). Match Details lists each full match, its index, and any named groups. Numbered capture groups are not listed there. An empty match is labeled (empty). Copy Matches writes one full match per line, without indexes or groups. Zero-length matches increment lastIndex so the search cannot loop forever.

When can the replace pane look empty or hide Copy?

The match and replace panes appear only when the test string is not empty. Copy Result is shown only when the replaced text differs from the original. If a replacement deletes everything (for example replacing a with an empty string under g), the pane shows the placeholder “Replace result will appear here…” because an empty string is treated as missing output — Copy Result still is not offered in that situation.

Is my text sent to a server?

No. Matching and replacement run in your browser as you type. Closing the tab discards the fields.

Related tools and guides

References