Skip to main content
Module 8 · Lesson 4 of 432/32 in the course~20 min
Module lessons (4/4)

Writing ReDoS-safe patterns

Lesson preview5 hands-on exercises readyRegex Course · Module 8 — Practice

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

  1. 1Mental model: combinatorial trees vs deterministic single path
  2. 24 progressive examples step by step
  3. 3Guided practical exercises
  4. 4Quick check
Pro Lesson · Module 8 — Practice

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.