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.

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

  1. Write the pattern without the surrounding slashes, and put the flags in their own field.
  2. Add g to see every match. Without it, only the first one is found, which is the most common reason a pattern “does not work”.
  3. 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.
  4. Try the replacement field before editing anything for real. $1 is the first group, $& is the whole match.
  5. 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 g flag only the first match is returned, and a global regex object keeps its lastIndex between 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 naive exec loop. 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 the s flag, which is the usual reason a multiline subject only half matches.
  • ^ and $ anchor to the whole string unless you add m.
  • 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?
The 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.
Is this the same as PHP’s preg_match?
Close, not identical. PCRE has features JavaScript lacks and spells named groups differently. Check in PHP before shipping a PHP pattern.
What does the backtracking warning mean?
Your pattern has a shape that can take exponential time on some inputs. It may be fine; it is worth knowing before it runs on user input.
Why does the match list stop at 500?
A cap, so a pattern matching every position in a long string cannot lock up the page.
Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.