Which regex flavour is this?
JavaScript (ECMAScript), as in browsers and Node.js. Most syntax is shared with Python, Java, PCRE and Go, but some isn't: lookbehind (?<=…) works here; possessive quantifiers a++ and atomic groups (?>…) don't; named groups are (?<name>…); in replacements use $1 and $<name> (Python uses \1).
Cheat sheet
\d \w \s | digit, word character, whitespace (capital = the opposite) |
|---|---|
. | any character except newline (with s: any character) |
* + ? {2,5} | 0+, 1+, 0 or 1, between 2 and 5 times; add ? to make them lazy |
^ $ \b | start, end (per line with m), word boundary |
[a-z] [^0-9] | character class, negated class |
(…) (?:…) (?<n>…) | capture, non-capturing, named group |
(?=…) (?!…) (?<=…) | lookahead, negative lookahead, lookbehind |
Why this tester stops slow patterns (ReDoS)
Patterns with nested quantifiers, like (a+)+$, can take exponential time on input that almost matches. OWASP calls this Regular expression Denial of Service: one request with crafted input can tie up a server. Here every test runs in a background worker and is stopped after 2 seconds, so the page never freezes. If a pattern is stopped, rewrite it before using it on user input.
Sources
- MDN: Regular expressions (JavaScript): developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
- OWASP: Regular expression Denial of Service (ReDoS): owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS