Regex Tester
Test a JavaScript regular expression against your own text: matches highlighted, capture groups listed, replacements previewed, backtracking flagged.
Matching runs in your browser, using its own regular expression engine. Enable JavaScript to see the matches.
Without the surrounding slashes. Flags go in the next field.
g finds every match, i ignores case, m makes ^ and $ match each line, s lets . match newlines.
Leave empty to skip. $1 and $2 are capture groups; $& is the whole match.
Contact [email protected] or [email protected].
Old address: [email protected] is no longer monitored.
After replacement:
Test a JavaScript regular expression against your own text. Every match is highlighted in place, capture groups are listed per match, and a replacement can be previewed before you commit to it.
How to use
- Write the pattern without the surrounding slashes, and put the flags in their own field.
- Add
gto see every match. Without it, only the first one is found, which is the most common reason a pattern “does not work”. - Read the group list under each match. Group numbers are counted by opening bracket, left to right, which is not always the order you expect.
- Try the replacement field before editing anything for real.
$1is the first group,$&is the whole match. - Heed the backtracking warning if it appears. A pattern that is fine on your test string can hang on a long one that nearly matches.
Example
Pattern: (\w+)@(\w+)\.(com|org)
Flags: g
Subject: Contact [email protected] or [email protected].
Match 1: [email protected] groups: ada, example, com
Match 2: [email protected] groups: grace, example, org
(com|org) is a capturing group, so it takes a number. If you only need the alternation and not the capture, (?:com|org) keeps the group count down and is slightly faster.
Pitfalls
- Without the
gflag only the first match is returned, and a global regex object keeps itslastIndexbetween calls, which makes reused patterns behave differently the second time. - A pattern that can match an empty string, such as
a*, loops forever in a naiveexecloop. This tool advances past zero-length matches, and caps the total at 500. - Nested quantifiers like
(a+)+backtrack exponentially on a subject that nearly matches. It is the standard denial-of-service shape, and it is why user-supplied patterns should never run on a server unbounded. - JavaScript regular expressions are not PCRE. Lookbehind needs a recent engine, and named groups are
(?<name>…)here and(?P<name>…)in PHP. .does not match a newline unless you add thesflag, which is the usual reason a multiline subject only half matches.^and$anchor to the whole string unless you addm.- Regular expressions cannot parse HTML. They can pull a value out of markup you control, and they will break on markup you do not.
- Escaping differs by language. A pattern written here has to have its backslashes doubled in a PHP or JSON string literal.
Compatibility
Matching uses your browser’s own regular expression engine, so the results are exactly what the same pattern does in JavaScript on that browser. Named groups, lookbehind and the s flag need Chrome 62, Firefox 78 or Safari 16.4 and later. PHP, Python and Go use different engines with different syntax in places, so a pattern verified here should still be checked in the language you are shipping it in. Nothing you type is uploaded.
Frequently asked questions
Why does it only find one match?
g flag is missing. Without it, matching stops after the first.Why is my dot not matching across lines?
. excludes newlines by default. Add the s flag.