gizmobench
BrowseDaily

Replace Text with Regex Capture Groups

Open a name-reordering example: (\w+), (\w+) and replacement $2 $1 transform Lovelace, Ada into Ada Lovelace. Capturing preserves selected text while the replacement changes its surrounding structure. Use $<name> for named groups or $& to retain the whole match. The operation remains JavaScript string substitution, with no callback execution or automatic case conversion.

$& keeps the match, $1 to $99 keep numbered groups, $<name> keeps a named group, and $$ inserts a dollar sign. Type an actual newline for a line break. Without g only the first match is replaced.

matchesmatching

Matching.

Replaced text

Replacing.

Matches
-
Groups
-
Flags
Engine
JavaScript

Matching.

d record where each match starts and ends · g find every match, not only the first · i ignore upper and lower case · m ^ and $ match at every line · s a dot also matches a newline · u read the text as unicode code points · v unicode sets, with set operations in classes · y match only where the last one left off

  • Two matchesglobal flag
    both, with offsets
  • Named groups(?<user>\w+)
    listed by name
  • Runaway patterncatastrophic backtracking
    stopped by timeout
Accuracy. Matching uses your browser's own JavaScript regular expression engine, so what you see here is what your code will do. It runs in a disposable worker with a timeout, so a pattern that would hang the page is stopped and reported instead.

Common questions

Can I use named captures?
Yes. (?<surname>\w+), (?<first>\w+) with $<first> $<surname> performs the same rearrangement. Group names and matched values appear in the match list.
Why does $10 sometimes keep group 1 followed by 0?
JavaScript uses group 10 if it exists. Otherwise, if group 1 exists, it uses group 1 followed by a literal 0. $01 uses group 1 when available; $0 is literal text rather than the whole-match token.
Can I lowercase or increment a capture?
A replacement string does not run code. Case conversion, counters and conditional computations require a replacement callback in your own program.

Matching uses your browser's own JavaScript regular expression engine, so what you see here is what your code will do. It runs in a disposable worker with a timeout, so a pattern that would hang the page is stopped and reported instead.