Regex Find and Replace
Test a regex replacement before running it on production data. The
replacement field supports $1, $2 for
capture-group references and $& for the full match.
Matches with capture groups
Replacement syntax
| Token | Meaning |
|---|---|
$& | The entire matched substring |
$1, $2, … | The 1st, 2nd, … capture group |
$<name> | The named capture group |
$` | Text before the match |
$' | Text after the match |
$$ | Literal $ |
Examples
Anonymize email domains
Pattern: (\b\w+@)\w+\.\w+
Replacement: $1example.com
Input: Email alice@gmail.com or bob@yahoo.co.uk
Output: Email alice@example.com or bob@example.com Reformat dates (US → ISO)
Pattern: (\d{1,2})/(\d{1,2})/(\d{4})
Replacement: $3-$1-$2
Input: Meeting on 4/26/2026 and follow-up 5/1/2026.
Output: Meeting on 2026-4-26 and follow-up 2026-5-1. Wrap log levels
Pattern: \b(ERROR|WARN|INFO)\b
Replacement: [$1]
Input: INFO request started; ERROR upstream timeout
Output: [INFO] request started; [ERROR] upstream timeout Surround code with backticks (markdown)
Pattern: \b(uuid|json|base64|regex|jwt)\b
Replacement: `$&`
Input: The uuid is a json field, base64-encoded.
Output: The `uuid` is a `json` field, `base64`-encoded. Programmatic equivalents
// JavaScript
text.replace(/(\b\w+@)\w+\.\w+/g, "$1example.com");
// Python
import re
re.sub(r"(\b\w+@)\w+\.\w+", r"\1example.com", text)
// sed (POSIX)
sed -E 's/(\b[a-z]+@)[a-z]+\.[a-z]+/\1example.com/g'
// Go
re := regexp.MustCompile("(\\b\\w+@)\\w+\\.\\w+")
re.ReplaceAllString(text, "${1}example.com") Common pitfalls
- Forgetting the
gflag. Without it, only the first match is replaced. The flag-toggle checkboxes above show the flags clearly. - Special characters in the replacement. Literal
$requires$$. A literal\\(in JavaScript source) requires\\\\— but the UI shows you the rendered replacement directly, so usually no escaping needed. - Capture-group numbering. Groups are numbered by their opening parenthesis order, including non-capturing wrappers. Use named groups
(?<name>...)for clarity. - Lookbehind / lookahead syntax differs. JavaScript supports
(?=...),(?!...),(?<=...),(?<!...)— but very old browsers may not. Use modern targets in production.
FAQ
How do I reference capture groups in the replacement?
Use $1, $2, etc. for the first, second capture group. $& is the entire match. $` is the text before the match, $' after. Named groups: $<name>.
Can I use a function as the replacement?
Not in this UI — but in JavaScript code, str.replace(re, (match, p1, p2) => ...) lets you compute the replacement from the match. The UI is limited to string replacements.
Is replacement greedy or lazy by default?
The regex itself controls greediness, not the replace operation. a.*b matches greedily; a.*?b matches lazily. The replacement just substitutes whatever was matched.
Without the g flag, does it replace all occurrences?
No — only the first. The g flag is required to replace all. The flag-toggle row above the editor makes this easy to set.