I am trying to debug a problem in my app which I have narrowed down to a particular situation involving a regular expression which causes Chrome to choke! Trying the same code in Firefox works fine. Also if I reduce my 'sample' text to run the regex on it works too.
So what gives?
Here is the jsfiddle: / (Which will fail to initialize at all because Chrome would choke if you are getting the same result as I am)
The code I have put in the jsfiddle is:
var rgx = /^(\d+([,|;]?\d*))*$/;
var sample = '40162690,40162755,40162691,40168355,40168357,40162726,40162752,40162729,40428707 ,40162740,40162546';
alert("Test is "+rgx.test(sample));
Maybe there is a better way to write my regular expression to avoid the issue? The goal is the regular expression should catch a string of numbers which are separated by ma or semi-colon.
I am trying to debug a problem in my app which I have narrowed down to a particular situation involving a regular expression which causes Chrome to choke! Trying the same code in Firefox works fine. Also if I reduce my 'sample' text to run the regex on it works too.
So what gives?
Here is the jsfiddle: http://jsfiddle/XWKRb/1/ (Which will fail to initialize at all because Chrome would choke if you are getting the same result as I am)
The code I have put in the jsfiddle is:
var rgx = /^(\d+([,|;]?\d*))*$/;
var sample = '40162690,40162755,40162691,40168355,40168357,40162726,40162752,40162729,40428707 ,40162740,40162546';
alert("Test is "+rgx.test(sample));
Maybe there is a better way to write my regular expression to avoid the issue? The goal is the regular expression should catch a string of numbers which are separated by ma or semi-colon.
Share asked Aug 1, 2013 at 15:43 TrantTrant 3,6117 gold badges41 silver badges58 bronze badges 4-
Is that space in
sample
deliberate? – Lightness Races in Orbit Commented Aug 1, 2013 at 15:45 - 1 I can confirm that this breaks Google Chrome v28 on Windows 7 64-bit – Lightness Races in Orbit Commented Aug 1, 2013 at 15:46
- This bug only seems to appear when a space occurs within the sample string. – Rion Williams Commented Aug 1, 2013 at 15:49
- yes the space was deliberate to test the fail condition – Trant Commented Aug 1, 2013 at 16:01
2 Answers
Reset to default 13You have a classic case of catastrophic backtracking:
^(\d+([,|;]?\d*))*$
^ ^ ^ ^
| | | ---- zero or more repetitions of the group
| | ------- zero or more digits
| ---------- zero or one ma, 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-match
1,1X 23
12,21X 119
123,321X 723
1234,4321X 4,743
12345,54321X 31,991
123456,654321X 217,995
1234567,7654321X attempt aborted after 1,000,000 steps
This pattern will work better:
var rgx = /^\d+(?:[,;]\s*\d+)*$/;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920057a4601076.html
评论列表(0条)