What the Regex Replace Tester Does
The Regex Replace Tester shows you the result of a find-and-replace operation before you commit it. You supply a subject, a pattern, and a replacement string; it runs the substitution and returns the transformed text, together with a length delta showing how many characters you gained or lost.
The problem it solves is specific. Find-and-replace against real data is not reversible in the way people assume. A pattern that is slightly too broad will quietly rewrite lines you never intended to touch, and by the time you notice, the original is gone — or worse, it is a migration script that already ran against a production column. Running the substitution against a representative sample first is the cheapest possible insurance.
It is equally useful for building the replacement string itself. Replacement templates have their own syntax for referring to captured text, and that syntax is where most silent failures happen: a template that inserts nothing, or inserts a literal dollar sign where a value was expected, produces output that looks plausible and is wrong.
How the Regex Replace Tester Works
The tool compiles your pattern with the flags you selected and hands it, along with the replacement string, to JavaScript's own String.prototype.replace. That means you are exercising the exact semantics your application code will use — no reimplementation, no dialect drift.
The single most important consequence of using the native method is how the g flag changes the operation. Without g, only the first match is replaced. With it, every match is replaced. The default here is g, which matches the behaviour most people expect from a replace utility, but it also means that toggling g off is how you get first-match-only behaviour — a distinction that matters enormously when you are normalising a file rather than editing one line.
Replacement strings are templates, not literal text. The engine interprets several sequences: $& for the whole matched substring, $1 through $99 for numbered capture groups, $<name> for a named group, $` for the text preceding the match, $' for the text following it, and $$ for a literal dollar sign.
That last point catches people repeatedly. A replacement intended to emit a currency amount must write $$ to produce one $. Written as a single $ followed by a digit it is read as a group reference — and if that group does not exist in the pattern, the engine substitutes an empty string instead of raising an error. You get a plausible result, a wrong one, and no diagnostic. This is precisely the class of mistake the length-delta readout is designed to expose: if you expected ten characters added and the counter reports a small negative number, the template did not do what you thought.
An invalid pattern does not produce output; the constructor throws and the error surfaces instead. The tool deliberately offers no inline diff, so the length delta and the transformed text are your two signals. When the substitution is subtle, verify it against a diff tool rather than reading the output line by line.
Using the Regex Replace Tester
- Step 1 — Load the subject text. Paste the content you want to transform into the main input area. Always use a realistic sample that includes at least one line you expect to match and one you expect to be left alone — a sample where everything matches proves nothing about the pattern's precision.
- Step 2 — Set the pattern. In the options bar, the first field is labelled Pattern (placeholder
pattern). Enter the pattern body without slashes or flags. - Step 3 — Set the replacement. The second field is labelled Replace (placeholder
replacement). This field takes a replacement template, so $1 and $& refer to captured text rather than literal characters. - Step 4 — Confirm the flags. The Flags row contains the six toggles
g, i, m, s, u, y, with g on by default. Turn g off to replace only the first occurrence. - Step 5 — Read the output and the length delta. The output panel holds the full transformed text. The info line reports the change as original-to-result character counts. A figure that is wildly off from your mental estimate is the fastest signal that the pattern matched more, or less, than you intended.
- Step 6 — Copy the result. The transformation is not applied to your input in place, so take the output via the copy control and treat the original as untouched.
Common Use Cases
Pre-flight a data migration. Renaming columns, renumbering records or rewriting identifiers is exactly the operation you want to rehearse on a sample. Because the length delta reports the aggregate change, a script that silently rewrote twice as many rows as expected shows up immediately.
Redact sensitive values before sharing. Replace email addresses, tokens or account numbers in a log excerpt with placeholders, and confirm from the output that nothing recognisable survived. Building the replacement template here is far safer than editing by hand.
Reshape captured groups into a new format. When a structured value needs to be reassembled differently — a date reordered, a name split, a log line converted to CSV columns — the replacement template does the work and the output shows the result directly.
Debug a replacement that is inserting nothing. An empty substitution where a group reference was expected is almost always a nonexistent group number or an unescaped dollar sign. Testing the template here isolates the cause in seconds.
Worked Example
Suppose you want to rewrite a log prefix into a cleaner form. Paste this subject:
INFO 2024-03-11 request 8821 completed
ERROR 2024-03-11 request 8822 failed
Set Pattern to (\w+) (\d{4}-\d{2}-\d{2}) request (\d+) and Replace to [$1] $2 req:$3, leaving g active. The output becomes:
[INFO] 2024-03-11 req:8821 completed
[ERROR] 2024-03-11 req:8822 failed
Every template reference did real work: $1 took the level, $2 the date, $3 the request number. Because g was on, both lines were rewritten.
Now the instructive failure. Change Replace to [$4] $2. Group four does not exist, so the engine substitutes nothing and the level annotation vanishes — no error, no warning. The output still looks like a plausible log line minus its severity, which is exactly why an unreviewed replacement template is dangerous. Watching the character delta is what turns that from a silent corruption into an obvious one.
A second variant shows the effect of the g toggle. With g off and the pattern unchanged, only the first line is rewritten and the second is returned untouched. If your intent was a single targeted edit, that is the correct configuration — but you must say so deliberately rather than by leaving the default in place.
Tips and Common Mistakes
- An unescaped dollar sign is the most common silent bug. To emit a literal
$ write $$. A single $ before a digit is read as a group reference, and a reference to a nonexistent group expands to nothing rather than throwing. - Remember that
g is on by default. The tool and most replace utilities default to replacing every match. If you want a single edit, toggle it off — do not assume the first match. - Test with negative cases. Include a line in your sample that should not match. A pattern proven only against matching input is untested.
- Watch the length delta. It is your only diff signal. A change far from your estimate means the pattern matched more than you intended, often because a quantifier is greedy or an anchor is missing.
- Check whether your pattern needs anchors. An unanchored pattern will rewrite matches embedded inside larger tokens, which is rarely what a field-level reformat wants.
- Do not rely on the replacement to validate. The template cannot reject input; it can only reshape whatever the pattern matched. Validate first, then transform.
- Beware of replacing into invalidity. A substitution can produce syntactically broken output — malformed JSON, an unbalanced quote, a duplicate key — even when the pattern matched correctly. Re-parse the result when the target is a structured format.
Frequently Asked Questions
Why was only the first occurrence replaced?
Because the g flag is off. String.prototype.replace replaces only the first match unless the regex is global. The flags row shows the current state — toggle g on to rewrite every match.How do I insert a literal dollar sign in the replacement?
Write $$. The replacement string is a template, so a lone $ that precedes a digit or a bracket is interpreted as a group reference rather than a currency symbol.Why did my replacement insert nothing at all?
Almost always one of two causes: the template references a group number the pattern does not define, or a dollar sign was meant literally and was read as a reference. Both expand to an empty string instead of raising an error, so the failure is silent — check the group count in the pattern, and confirm the template's references against it.Can I use named groups in the replacement?
Yes, if your pattern defines them. Write the group as (?<name>...) in the pattern and refer to it as $<name> in the replacement. Named references are more readable than positional ones and survive pattern edits that reorder groups.Is my input modified or sent anywhere?
Neither. The substitution runs in the page using the browser's own regex engine, and your subject text is not altered in place — you copy the result out. Nothing is uploaded.Related Tools
Before replacing, confirm what your pattern matches with the
Regex Tester — it reports each match and its capture groups, which tells you the group numbers your replacement template can safely reference. To review the result of a subtle substitution, the
Diff Checker shows the changed regions directly. If you need to understand a pattern's components rather than its effect, the
Regex Explainer lists the token types it recognises.
Privacy & Security
This tool runs entirely in your browser — no data ever leaves your device. There is no server round-trip, no upload, no logging, and no account required. Your input is processed locally using client-side JavaScript and is never stored, transmitted, or accessible to anyone else. When you close the tab, everything disappears.