Regex Lab
Test JavaScript regular expressions with live matches and groups. Free online regex tester for developers.
JavaScript RegExp flags
g— global; find all matches; mutateslastIndexi— ignore casem—^/$match line starts/endss— dotAll;.matches newlinesu— Unicode; proper code-point escapesy— sticky; match only atlastIndexd— indices; expose match start/end offsets
Worked example
const re = /\b(\w+)@(\w+)\.(\w+)\b/g; const s = "a@x.com b@y.org"; [...s.matchAll(re)].map(m => m[0]); // → ["a@x.com", "b@y.org"] // Catastrophic-style pattern (do not run on huge input): // /^(a+)+$/ against "aaaaaaaaaaaaaaaaaaaaX"
Lookbehind and ReDoS
Variable-length lookbehind is supported in modern V8/SpiderMonkey but remains a portability footgun. For user-controlled patterns or inputs, length-limit the string and avoid nested +/* on the same character class.
Frequently Asked Questions
Why does my pattern match once without the g flag but “stall” in a loop?
With g, lastIndex advances on the RegExp object. Reusing the same /pat/g instance across exec calls is stateful. Create a fresh RegExp or reset lastIndex when looping.
Does JavaScript support lookbehind?
Yes in modern engines: (?<=...) and (?<!...). Older browsers may throw. Prefer feature detection or transpile carefully for legacy targets.
What is catastrophic backtracking?
Nested quantifiers like (a+)+$ on long non-matching strings can explore an exponential number of paths and freeze the tab. Prefer possessive-style thinking, atomic groups where available, or simpler parsers for untrusted input.