Regex Tester
Test and debug regular expressions in real time
A field guide to the regex tester
This tester runs your pattern through the browser's native JavaScript engine, so whatever matches here matches the same way in your production code. I keep reaching for the same four topics whenever I write a regex, so I condensed them into the reference below.
The flags that matter
Four flags are exposed as toggles. The g flag finds every match instead of stopping at the first, and i ignores case; together they cover most day-to-day work. The m flag makes the caret and the dollar-sign anchor match at each line boundary rather than only at the ends of the whole input, and s lets the dot cross newlines. JavaScript also defines u for Unicode handling and y for sticky matching, which this tester does not put on a toggle.
Capture groups, numbered and named
Wrap part of a pattern in parentheses and the engine captures whatever that part matched, numbered left to right, and the results panel lists each one as Group 1, Group 2, and so on. JavaScript also supports named groups written (?<name>...), and they match fine here, though the panel still shows captures by position rather than by name. Named groups earn their keep in your own code: insert a new group into the middle of a numbered pattern and every later back-reference shifts by one, while a named reference stays put.
The pattern that hangs a tab
Certain patterns force the engine to try an exponential number of paths before it gives up. The classic shape is a quantifier nested inside another quantifier, such as (a+)+b run against a long string of a characters: each failed attempt makes the engine re-partition the input and start over. In a plain browser context that freezes the tab. Here the match runs inside a Web Worker with a 1-second timeout, so a pathological pattern aborts with a warning while the page stays responsive. The guard has a cost, though: a very long input can hit the timeout before a legitimate match finishes. When that happens, treat it as a sign the pattern wants restructuring.
Everyday patterns worth memorizing
A few small patterns cover a large share of daily work. \d+ grabs runs of digits. ^\s+ and \s+ followed by the dollar-sign anchor find leading and trailing whitespace for trims. For email I use a loose shape like [^\s@]+@[^\s@]+\.[^\s@]+, which asks for one @ sign and a dot in the domain, nothing more. A regex that fully validates email against the RFC does not exist in any usable form; real addresses permit comments, quoted local parts, and other oddities no sane pattern covers. Match the loose shape, then confirm the address by sending to it.
Sharp edges
- Which regex dialect does this use?
- JavaScript regex, ECMAScript 2018 and later. It sits close to PCRE (the dialect PHP and grep use) but is not identical: look-behind works, since it landed in 2018, while PCRE-specific extensions such as conditional patterns are unavailable. If a pattern comes from a PHP codebase or a shell script, test it here before trusting it in JavaScript.
- Why does my pattern match more text than I expect?
- The * and + quantifiers are greedy by default and consume as much as possible. Append a question mark (*? or +?) to make them lazy, so they consume as little as possible. Greedy overreach is the most common regex bug I see from newcomers.
- How do I match a literal dot or other special character?
- Escape it with a backslash. The characters that need escaping are . + * ? ( ) [ ] { } ^ $ | and the backslash itself. Matching the literal string 3.14 takes the pattern 3\.14; without the backslash the dot matches any character, so 3714 would pass too.