Regex tester
The tester uses your browser's own JavaScript engine, so your pattern follows JavaScript's rules, which differ from PCRE's and Python's in places. You'll see up to 1,000 matches, each with its groups. It runs in your browser; nothing you enter is uploaded.
What the flags do
gfinds every match instead of stopping at the first.iignores case, socatmatches "Cat".mmakes^and$match at the start and end of each line, not only of the whole text.slets.match a line break too.-
ureads the pattern as Unicode code points, so.matches a whole emoji and\p{L}matches any letter. It also makes unnecessary escapes such as\aan error. yis sticky: each match must start exactly where the previous one ended, and the first one must start at position 0.
Every current browser supports all six, along with named groups and lookbehind, which arrived with s in ES2018. If your text has emoji or letters outside
English, turn on u. The MDN regular expressions guide
covers the full syntax.
Reading the matches
The index is where the match starts, counting from 0 in UTF-16 code units. It's the same number match.index gives you in code. Most characters are one unit,
but an emoji such as 😀 is two, so every index after it is one higher than the number of characters you see.
Groups are numbered by their opening parenthesis, left to right, and the tester also lists a named group such as (?<year>…) under its name. With the
example pattern, the first match is 2024-03-15 at index 9. Group 1, named year, is 2024; group 2, named month, is 03; and group 3 is
15.
A group that took no part in the match, such as the second group of (a)|(b) when the text is "a", shows as "(no match)". A group that matched an empty
string shows as "(empty)" instead. Without the g flag, the tester lists only the first match, the one exec() would return.
Empty matches
Patterns such as a*, ^, or \b can match zero characters. If the tester searched again from the same spot, it would find the same
empty match forever. So after an empty match it moves on one position, or one whole code point with the u flag. String.prototype.matchAll does
the same, so you get exactly the matches it would return: a* against "baac" gives an empty match at 0, aa at 1, and then empty matches at 3 and 4.
Pitfalls
-
Python's named group syntax
(?P<name>…)is an error in JavaScript; write(?<name>…). The backreference is\k<name>. -
JavaScript has no
\Aor\Z. Without theuflag,\Amatches the letter A and gives no error. Use^and$without themflag instead. -
\dmatches only 0 to 9, and\wonly A to Z, a to z, 0 to 9, and the underscore, so neither matches é or Arabic digits. With theuflag,\p{Nd}and\p{L}match digits and letters in every script. -
Nested quantifiers such as
(a+)+$can take exponential time on text that almost matches, like a long run of a's ending in "!". The tester runs your pattern in this tab as you type, so a pattern like that can freeze the tab. On a server, the same pattern is a denial-of-service risk.
Using the pattern in code
const re = /(?<year>\d{4})-(?<month>\d{2})-(\d{2})/g
for (const m of text.matchAll(re)) {
console.log(m.index, m[0], m.groups.year)
}
text.replace(re, '$<month>/$3/$<year>') // "Released 03/15/2024, …" matchAll needs the g flag and throws an error without it. If you only need to know whether the text matches, use re.test(text).
But with g or y, the regex remembers where it stopped in re.lastIndex, so calling test twice on the same text can give
different answers.