Why does this code cause Chrome to choke? Why does this code cause Chrome to choke? google-chrome google-chrome

Why does this code cause Chrome to choke?


You have a classic case of catastrophic backtracking:

^(\d+([,|;]?\d*))*$    ^      ^  ^  ^    |      |  |  ---- zero or more repetitions of the group     |      |  ------- zero or more digits    |      ---------- zero or one comma, pipe or semicolon    ----------------- one or more digits

contains a repeated group which contains optional elements, one of which is repeated itself. Ignoring the separators for now, you have essentially the regex

^(\d+\d*)*$

That leads to an exponential number of permutations your regex has to check in the worst case.

As soon as another character besides the allowed characters is found in your string (like a space in your example), the regex must fail - but it takes the engine ages to figure this out. Some browsers detect such runaway regex matches, but Chrome appears to want to ride this out.

To illustrate this, testing your regex in RegexBuddy shows the following:

Input             Steps to determine a non-match1,1X                   2312,21X                119123,321X              7231234,4321X          4,74312345,54321X       31,991123456,654321X    217,9951234567,7654321X  attempt aborted after 1,000,000 steps


This pattern will work better:

var rgx = /^\d+(?:[,;]\s*\d+)*$/;