Skip to content

no-redos ​

A regular expression that can take exponential time on some inputs.

Why: V8's regex engine backtracks. When a repetition can match the same text in several ways, a failing match tries all of them: /^(\w+\s?)+$/ on 30 word characters followed by ! takes minutes, and blocks the thread (a request handler, the UI) the whole time. V8 has a linear-time engine, but it is experimental and off by default (v8.dev).

✕ Rejectedno-redos
ts
export function isWords(input: string): boolean {
  return /^(\w+\s?)+$/.test(input);
}

✅ Accepted: each character can only be matched one way

✓ Accepted0 diagnostics
ts
export function isWords(input: string): boolean {
  return /^\w+(?:\s\w+)*$/.test(input);
}

See it happen (each step adds two letters; the demo stops when one step takes over 1.5 s):

/^(\w+\s?)+$/ vs /^\w+(?:\s\w+)*$/ on "aaa…a!"

The rule flags an unbounded repetition whose body can repeat ambiguously: a repetition inside it whose characters can also start what follows ((a+)+, (\w+\s?)+, (.*)*), or alternatives that can start with the same character ((a|aa)+). Character sets are compared, negated ones too: in (?:[^"]|"[^"]*")* the first alternative can't start with the " the second starts with, so each character is matched one way. It checks regex literals and new RegExp("literal").

✅ Accepted: quoted values in a list of attributes

✓ Accepted0 diagnostics
ts
export const attributes = /(?:[^<>"]|"[^"]*")*/g;
export const tag = /<(\/?)([A-Za-z][\w:-]*)((?:\s+[\w:-]+="[^"]*")*)\s*(\/?)>/;

What the rule can't see: patterns built from strings at runtime, and the polynomial cases (\d+\d+$, .*.*=, (?:\s+a)*\s* in the tag above), which are slow on long input but not catastrophic; only exponential ones are reported. When a pattern must stay ambiguous, limit the input length before matching.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra