Module lessons (4/4)
Writing ReDoS-safe patterns
Recognize catastrophic patterns ((a+)+b), refactor them, use atomic-like quantifiers.
What you'll learn in this lesson
- Recognize the causes of ReDoS (Regular expression Denial of Service) in backtracking regex engines.
- Immediately spot risky patterns: nested quantifiers (
(a+)+), overlapping alternatives ((a|ab)*), and ambiguous separators ((\w+\s*)+). - Refactor an exponential regex into a linear, disjoint version (
a+,(?:\w+\s+)*\w+) or delimit it with negated classes ([^"]*).
Overview
ReDoS (Regular expression Denial of Service) is a real-world vulnerability in which a short, purposefully crafted input freezes the CPU for seconds, minutes, or hours. In backtracking engines like JavaScript, an ambiguous regex tries an exponential number of combinations before giving up on a string that does not\_ match.
When a pattern offers multiple identical ways to split the same characters (e.g. (a+)+ in front of "aaaaaaaaaaaaaaaaaaaa!"), each partial success multiplies the backtracking branches (2^N!). Eliminating the ambiguity by transforming the repeated parts into mutually exclusive (disjoint) choices brings the execution back to linear time O(N).
Sections covered in this lesson
- 1Mental model: combinatorial trees vs deterministic single path
- 24 progressive examples step by step
- 3Guided practical exercises
- 4Quick check
Unlock "Writing ReDoS-safe patterns" and the full Regex Course curriculum
Recognize catastrophic patterns (`(a+)+b`), refactor them, use atomic-like quantifiers.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Regex Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.