Level 3: Advanced · Lesson 23 of 30
ReDoS and Catastrophic Backtracking
Some patterns take exponential time on certain inputs. Learn to spot nested quantifiers and overlapping alternatives, and how RegexLab protects you with a timeout.
Most regex engines, including those in JavaScript, Python, Java and PHP, use backtracking. When a path fails, the engine steps back and tries another way to split the text between quantifiers. Usually there are only a few ways, but some patterns create an enormous number of them. When the input almost matches but then fails, the engine tries every combination before giving up. This is called catastrophic backtracking.
The textbook danger sign is a quantifier inside a quantifier where both can match the same characters, such as (a+)+$. A string of thirty a characters followed by a b can be split into groups in over a billion ways, and the engine tries them all. Overlapping alternatives inside a repetition, such as (\w|\d)+, cause the same blow-up because a digit can be matched by either branch.
When an attacker can send input to such a pattern on a server, a single request can freeze a worker for seconds or minutes. This is a regular expression denial of service, or ReDoS. Real outages have been caused by one innocent-looking validation pattern.
The fix is to make every character have only one sensible way to be matched. Remove the nesting: (a+)+ is just a+. Make repeated units start with a required separator, as in \d+(?:,\d+)*, where each repetition must begin with a comma. Use bounded quantifiers like \d{4,8} instead of open-ended ones when you know the limits, and anchor validation patterns with ^ and $.
Some engines offer atomic groups (?>...) or possessive quantifiers a++, which forbid backtracking into a group. JavaScript has neither, so in JavaScript the rewrite is the only defence. Go and Rust avoid the problem entirely by using engines that guarantee linear time, at the cost of lookarounds and backreferences.
RegexLab runs every pattern you type inside a Web Worker with a 500 ms timeout. If a pattern takes longer, the worker is stopped, the page stays responsive, and you see a warning instead of a frozen tab. The tool also flags nested quantifiers it detects while you type.
A timeout is a safety net, not a fix. If RegexLab warns you, rewrite the pattern before shipping it. The examples on this page are deliberately safe versions of patterns that are often written dangerously.
Live tester
2 matches (JavaScript engine)
1,22,333 4,5 abc 7,,8
Examples
Numbers
/\d+(?:\.\d+)?/Values 3.14 and 42 and 0.5
Matches: “3.14”, “42”, “0.5”
One optional fractional part and no nested repetition keep this pattern linear.
Text
/[a-z]+(?:-[a-z]+)*/well-known and up-to-date words
Matches: “well-known”, “and”, “up-to-date”, “words”
Each repeated unit must start with a dash, so there is only one way to split the text.
Codes
/\b[A-Z]{2}\d{4}\b/Codes AB1234 and CD5678 are valid.
Matches: “AB1234”, “CD5678”
Fixed counts leave the engine nothing to backtrack over.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match comma-separated number lists safely, with each repetition starting at a comma.
1,2,3 and 10,20
Task 2: Match lowercase slugs separated by single dashes. A double dash must break the slug.
my-first-post, draft--bad, ok
Task 3: Match standalone IDs of 4 to 8 digits using a bounded quantifier.
ID 1234567 and 12 and 123456789012