Why can a regex take far longer to reject a string than to accept one? The answer can be backtracking: when a later part of a pattern fails, the regex engine revisits an earlier choice and tries a different way to match. section 12.6.3
Giving a character back
Consider /[1-9][0-9]*\s+[a-z]+s/ against "27 dresses". The greedy [a-z]+ first consumes all of "dresses", leaving nothing for the final s. The engine gives back the last character: [a-z]+ now matches "dresse", and s matches the remaining character. section 12.6.3
A greedy + or * consumes as much as it can, but its first choice is not necessarily final. The engine can try shorter matches when a subsequent term fails. A long word with an s near its beginning may require many retries before this pattern finds its match; individual retries are cheap, but the number of possible choices matters. section 12.6.3
Listing 12.13 A regex with catastrophic backtracking
const rosterRegex = /^(?:\w+\s*)+$/; #A
console.assert( #B
rosterRegex.test("Hattori Yoshi Fuma"), #B
"A genuine roster is accepted instantly", #B
); #B
const attack = #C
"I will erase your existence from this realm!"; #C
const start = performance.now(); #C
rosterRegex.test(attack); #C
const elapsed = performance.now() - start; #C
console.log(
`Rejecting the attack string took ${elapsed.toFixed(0)}ms`,
);
The roster test uses /^(?:\w+\s*)+$/ and times its rejection of a nonmatching string. It demonstrates the performance danger: a pattern with exponentially many backtracking possibilities can spend a long time failing, blocking the JavaScript thread until those possibilities are exhausted. section 12.6.3 section 12.7








