Join our Newsletter — 33% off our NHI Course

Why do some regular expressions consume so much CPU when they fail to match?

The risk comes from backtracking. When a pattern has repeated elements, optional separators, or overlapping choices, the engine may try many different matching paths before concluding there is no match. On long near-miss inputs, that search can become quadratic or even exponential, which is enough to block a JavaScript event loop or stall a browser thread.

Why regex engines slow down on near-misses

Most expensive failure cases come from backtracking, not from the mere fact that a match failed. If the pattern contains repetition, nested optional pieces, or overlapping alternatives, the engine may keep revisiting the same input positions through many different paths before it can prove there is no match. That search can grow dramatically on long near-miss strings.

A pattern like this is especially costly when the input almost fits, because the engine first commits to one path and only discovers the mismatch late. At that point it has to unwind and try the next possibility, then the next one, multiplying work across repeated sections. The result is often quadratic behaviour, and in the worst cases exponential behaviour.

This matters because the failure cost can dominate the success cost. A regex that looks harmless in normal traffic can become a CPU sink on adversarial or simply unlucky inputs, which is why these bugs show up as stalled browser tabs, blocked JavaScript event loops, or sudden latency spikes in services that validate text synchronously.

For a broader view of how pattern design can create expensive search paths, the same class of failure is discussed in the OWASP API Security Top 10 as unrestricted resource consumption, even though the mechanism here is regex backtracking rather than an API quota issue.

Which pattern features create the worst backtracking

The main danger signs are repeated groups that can match the same characters in more than one way. Common examples include (.+)+, ambiguous alternations such as (a|aa)+, and sequences where optional separators or wildcards can be assigned to different parts of the pattern. Each extra ambiguity gives the engine another branch to explore when the match starts failing.

Nested quantifiers are usually the worst offender because they combine two sources of ambiguity. An inner repetition can match a span in several lengths, and an outer repetition can then regroup those spans in several ways. On a long input, especially one designed to miss only at the end, the engine may end up testing a huge number of equivalent-looking paths.

Anchoring and simplification usually help more than micro-optimising syntax. A tighter pattern reduces the search space; a more specific token class or delimiter often removes the ambiguity entirely. If the regex is used on untrusted input, the practical question is not whether it is elegant, but whether it can be forced into a long failure path.

Engine choice matters too. Backtracking engines are expressive but vulnerable to this class of slowdown, while linear-time engines avoid many of these pathological cases by construction. For practitioners, that means you should review the pattern and the engine together, not assume that a valid regex is automatically safe.

Risk and Threat Considerations

Regex backtracking becomes a denial-of-service problem when an attacker can control the input and trigger a worst-case failure path. The risk is highest where validation runs on the request path, inside a browser thread, or anywhere a single expensive match can monopolise CPU long enough to delay other work.

Failure mechanism: The engine explores many overlapping parse paths before it can conclude there is no match, and near-miss inputs force it to exhaust those paths at high cost.

Impact: CPU spikes, latency blowouts, event-loop blocking, and in some cases service degradation across multiple requests if the regex is shared or executed repeatedly on large inputs.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Agentic AI Top 10 and MITRE ATT&CK address the attack and risk surface, while CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Agentic AI Top 10 A1 — Unrestricted Resource Consumption Regex backtracking can monopolise CPU and block execution.
Recommendation — Bound regex execution and reject patterns with unbounded failure-time growth.
CIS Controls v8 10 — Data Recovery Regex stalls can degrade availability and require operational recovery.
Recommendation — Test validation logic for worst-case runtime before deployment.
NIST CSF 2.0 PR.IP — Information Protection Processes and Procedures Safe regex design belongs in secure coding and validation procedures.
Recommendation — Standardise secure regex review and performance testing in development.
MITRE ATT&CK T1499 — Endpoint Denial of Service Catastrophic regex backtracking can be abused to exhaust CPU and deny service.
Recommendation — Monitor for user-controlled inputs that trigger repeated regex evaluation.

Practitioner Guidance

What to verify: Test the exact regex against long near-miss inputs, not just successful cases. The relevant question is whether failure time grows sharply as the input length increases, because that is where catastrophic backtracking shows up.

Decision rule: If a pattern will run on untrusted or user-controlled text, treat ambiguous repetition as a security and reliability issue, not a style issue. Prefer a simpler pattern, a linear-time engine where available, or an alternative parsing approach when the failure path cannot be bounded confidently.

Common mistake: Teams often benchmark only the happy path and assume the regex is safe because valid inputs are fast. The dangerous behaviour is usually hidden in mismatches, especially near-misses that reach the end of the string before failing.

Practitioner takeaway: A regex is safe only when both the successful match and the failure path are predictable, because the failure path is what adversaries and accidental edge cases can turn into a CPU attack.