Regex Replace Tester
Preview JavaScript regular expression replacements on sample text.
Your result will appear here…
How to use Regex Replace Tester
- Enter the Regular expression and the Flags; g is filled in by default.
- Enter the Replacement and paste the sample text.
- Select Replace and read the result.
Example
Input / settings:
Regular expression: user_(\d+) Flags: g Replacement: account-$1 Text: user_123, user_456
Output:
account-123, account-456
$1 inserts the first capture group, so each number is kept and the prefix changes.
When to use
- Preview a find-and-replace before you run it on real files.
- Test capture-group replacements such as $2 $1.
- Check how a flag changes which matches are replaced.
Overview
Regex Replace Tester runs JavaScript's String.replace with the pattern, flags and replacement you enter on sample text of up to 5,000 characters, with patterns of up to 250 characters. It shows only the replaced text. The replacement is a string that can use JavaScript's special tokens such as $1 and $&; it is not a function, and the engine is your browser's, not PCRE or Python.
Supported input
pattern, replacement, and text
Output details
replaced text
Common mistakes
- Without the g flag only the first match is replaced. user_(\d+) with empty flags changes user_123 and leaves user_456 alone.
- Replacement tokens follow JavaScript. $$ is a dollar sign, $& is the match, and $` and $' are the text before and after it. $0 is not a group, so it stays literal, and so does $2 when the pattern has one group; with one group, $10 is group 1 followed by a 0.
- A backslash in the replacement is literal. \n inserts a backslash and an n, not a line break; type a real line break in the field instead.
- Empty matches are replaced too. a* on baac gives -b--c- with replacement -, and an empty pattern puts the replacement between every character.
- Nothing stops catastrophic backtracking. (a+)+$ on 24 letters a followed by b took about two seconds in a test run, the time grows exponentially with the length, and the page can freeze meanwhile. The size limits do not prevent it.
Practical tips
- Use Regex Tester first to see each match and its position before you replace.
- Named groups are easier to read in the replacement: with (?<first>\w+) (?<last>\w+), $<last> $<first> swaps the two words.
Privacy and security
This tool runs entirely in your browser. Your input is never uploaded to our servers. Replacement runs locally with your browser's regular expression engine and nothing is uploaded. Be careful with patterns copied from untrusted sources: nested repetition can make the tab unresponsive, as the OWASP ReDoS page explains.
References
- MDN: String.prototype.replace()
- MDN: Regular expressions
- OWASP: Regular expression Denial of Service (ReDoS)
Frequently asked questions
Which flags does Regex Replace Tester accept?
Any flags your browser's RegExp accepts, such as g, i, m, s, u and y. A repeated flag like gg or an unknown one like z returns an error.
What are the size limits?
The pattern can have up to 250 characters and the sample text up to 5,000. Longer input is rejected with a message.
Can the replacement be a function?
No. The replacement is plain text with the special tokens $1, $&, $<name>, $$, $` and $'.