What this tester runs
The pattern is compiled with new RegExp(pattern, flags) in your own browser, so the behaviour is exactly what your JavaScript or TypeScript code will see in that browser. Lookbehind, named groups and Unicode property escapes work where your browser supports them. The v flag is newer and needs a recent Chrome, Firefox or Safari, and it cannot be combined with u.
The flags change how a pattern behaves:
- g finds every match. Without it only the first match is returned.
- i ignores case. m makes
^and$match at each line. s lets the dot match line breaks. - u and v treat the text as Unicode code points, which matters for emoji and for
\p{L}. Without them an emoji counts as two characters. - y matches only at the exact position where the search starts. d records where each group matched, which this page uses for the group positions.
If you are coming from another language, expect differences. JavaScript has no atomic groups or possessive quantifiers, no \A or \Z, and no inline flag at the start of a pattern such as (?i). Named groups are written (?<name>...), not (?P<name>...). Test the pattern in the engine you will actually use.
Catastrophic backtracking, and why the test runs in a worker
A regular expression engine tries alternatives and backs up when one fails. With a pattern like (a+)+$ on a long run of the letter a followed by a b, the number of ways to split the a's doubles with every extra letter. A few dozen characters can keep the engine busy for minutes. When this happens on a server it is called ReDoS, and it can take a service down.
JavaScript cannot interrupt a regex that is already running, so this page runs each test in a separate worker and stops that worker after 2 seconds. You get an explanation and a working page instead of a frozen tab. The worker is replaced on the next keystroke.
To make a slow pattern safe in your own code: avoid a repeat inside a repeat over the same characters, make alternatives not overlap, anchor the pattern, and limit the length of the text you test it against.
Capture groups and replacements
Parentheses capture part of a match. (\d{4})-(\d{2})-(\d{2}) captures the year, month and day of a date as groups 1, 2 and 3. Give a group a name with (?<year>\d{4}) to read it by name instead of by number.
In a replacement template $1 is the text of group 1, $<year> is the text of a named group, $& is the whole match and $$ is a literal dollar sign. The example loaded on this page turns 2024-05-12 into 12/05/2024. Code that needs to compute the replacement can pass a function to replace, which this preview does not cover.
A group that is inside an alternative that did not match, such as the first group in (a)|(b) when the text is b, is reported as undefined. This page labels it did not match.
Common mistakes
- Forgetting to escape. A literal dot, bracket or plus sign needs a backslash:
\.matches a full stop, while.matches almost anything. - Greedy repeats.
<.+>swallows everything from the first<to the last>. Use<.+?>for the shortest match, or<[^>]+>. - Expecting
\wto match accented letters. It only coversAtoZ,atoz, digits and the underscore. Use\p{L}with theuflag for letters in any language. - No g flag. If only one match shows up, the g flag is off.