Find and Replace (Regex)
Replace text using a JavaScript regular expression, with support for $1, $2 backreferences.
Flags(g = all, i = case-insensitive, m = multiline)
0 replacementsAbout Find and Replace (Regex)
Regex find-and-replace lets you match text by pattern instead of literal value, and reference matched groups in the replacement via $1, $2, etc. Patterns use JavaScript's regex flavor (ECMAScript). Flags (g, i, m, s, u, y) are configurable. Replacement supports $& (full match), $` (text before), $' (text after), and $N (capture group N).
When to use it
- Reformatting data — e.g., swapping 'Last, First' to 'First Last' (/(\w+),\s*(\w+)/ → $2 $1)
- Extracting and reformatting parts of a string
- Bulk-fixing complex patterns that literal find-replace can't catch
- Adapting a sed-style one-liner for use in the browser
How it works
The find pattern is compiled with new RegExp(pattern, flags). The replace string is processed for backreferences ($1, $&, etc.) before each match is substituted. Invalid regex is reported with the JavaScript engine's error message.
Examples
Swap first and last name
find=(\w+), (\w+) replace=$2 $1\ninput="Lovelace, Ada"
"Ada Lovelace"
Wrap numbers in brackets
find=\b(\d+)\b replace=[$1]\ninput="3 apples and 4 oranges"
"[3] apples and [4] oranges"
Frequently asked questions
- What regex flavor is used?
- JavaScript (ECMAScript) regex. PCRE-only features like lookbehinds with variable width, possessive quantifiers, or recursion aren't supported. Lookbehinds with fixed width work (supported since ES2018).
- What flags are available?
- g (global, replace all), i (case-insensitive), m (^ and $ match line boundaries), s (. matches newlines), u (Unicode-aware), y (sticky). The default is 'g'.
- How do I escape special characters?
- Use a backslash: \. matches a literal dot, \\ matches a backslash. To match a literal $ in the replacement, write $$.
- Is my regex compiled safely?
- Yes — JavaScript's regex engine is safe to run on untrusted patterns, with one caveat: catastrophic backtracking on pathological patterns can hang your tab. If the page freezes after typing a pattern, refresh and write a more constrained regex.