Avoiding Catastrophic Backtracking (ReDoS)
Regular expressions are great for extracting structured fields from logs and validating input formats, but nested quantifiers like (a+)+$ or overlapping wildcards like .*.* can force JavaScript's backtracking engine into exponential work on non-matching strings.
// Safer explicit pattern for ISO dates (YYYY-MM-DD) with named capture groups
/^(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])$/Practical Rules for Production Regexes
- Use named capture groups (
(?<name>...)) so match results and replacement templates ($<name>) remain readable months later. - Prefer negated character classes (like
[^"]+) and bounded quantifiers (like\d{1,4}) over greedy wildcards. - Never combine
uandvflags — choosevwhen you need Unicode set intersection/subtraction, orufor standard Unicode code-point matching. - Click Test 500 ms ReDoS guard in the tool above to watch
(a+)+$against 28acharacters +!terminate safely without lagging the page.
Limits
This tester uses your browser's native JavaScript (ECMAScript) RegExp engine via matchAll() (handling Unicode emoji safely). Patterns are capped at 5,000 characters, match display is capped at 500 matches with a total count banner, and every run is bounded to 500 ms after worker startup.