Regex replace with capture groups
Rearrange text by capturing parts of each match and writing them back in a new order. Refer to numbered groups as $1 and $2, named groups as $<name>, and the whole match as $&, and the result updates as you type. All processing runs in your browser.
MODE
How it works
The tool uses the browser's native JavaScript RegExp engine. Patterns are compiled with new RegExp(pattern, flags) and run in a Web Worker that is stopped after 2 seconds, so a runaway pattern cannot freeze the page. Invalid patterns show an error message next to the pattern and never crash the page.
Processing runs in your browser
Both the pattern and the test string are processed entirely in your browser. Our servers are not involved at any point.
Related operations
For plain substring swaps without regex, try find and replace. To validate JSON patterns you are matching, use the JSON formatter. For decoding escape sequences in HTML attribute values, see HTML entities.
Frequently asked questions
- How do I swap two words with capture groups?
- Capture each word and write them back in the new order: the pattern (\w+) (\w+) with the replacement $2 $1 turns hello world into world hello.
- How do I use named groups in the replacement?
- Name the group in the pattern with (?<name>...) and refer to it as $<name>. The pattern (?<year>\d{4})-(?<month>\d{2}) with the replacement $<month>/$<year> turns 2026-09 into 09/2026.
- How do I insert a literal dollar sign?
- Write $$. A $ before a digit, <, &, ` or ' can be read as a group or match reference, so write $$5 when you want the literal text $5.
- Why does $0 not insert the whole match?
- JavaScript has no $0 reference, so $0 stays in the output as literal text. Use $& to insert the whole match.
Last reviewed September 30, 2026
Other pages for this tool
Related tools
- Background reading: Complete guide to developer tools