Recently, when modifying a compatibility bug, we found a bug caused by browser optimization. First look at the example.
Alert (validatedirtychar (input ));
}
);
$ ( " # Btnchecknewregexp " ). Click (
Function (){
VaR Input = $ ( " # Textname " ). Val ();
Alert (validatedirtycharnewregexp (input ));
}
);
}
);
// Regular Expression Test
Function Validatedirtychar (input ){
If (Input = '' ) Return False ;
VaR Result = False ;
// 1 to 12 characters
VaR Reg = / ^ [A-Za-Z] {1, 12} $ / G;
Result = Reg. Test (input );
Return Result;
}
// Non-Direct amount regular expression test
Function Validatedirtycharnewregexp (input ){
If (Input = '' ) Return False ;
VaR Result = False ;
VaR Reg = New Regexp ( " ^ [A-Za-Z] {1, 12} $ " , " G " );
Result = Reg. Test (input );
Return Result;
}
The regular expression is very simple, that is, to test whether it is an English character or a length of 1-12, but it is such a simple code. Enter the correct input and test it on chorme and ff, A strange problem may occur when you click the "quantity button" button twice. The results of the two tests are inconsistent. The results are correct for the first time, but fail for the second time. Why are the results of the two tests inconsistent? After you click the 2nd tests with no direct quantity, you can find that the results are correct even if you click the test button multiple times. Why?
The string Methods Search () , Replace () , And Match () Do not use Lastindex Property Exec () And Test () Do. In fact, the string methods simply Reset Lastindex () To 0. If you use Exec () Or Test () On a pattern that has G Flag Set, and you are searching multiple strings, you must either find all the matches in each string so that Lastindex Is automatically reset to zero (this happens when the last search fails), or you must explicitly set Lastindex Property to 0 yourself. If you forget to do this, you may start searching a new string at some arbitrary position within the string rather than from the beginning. Finally, remember that this special Lastindex Behavior occurs only for regular expressions with G Flag. Exec () And Test () Ignore Lastindex Property of Regexp objects That do not have G Flag.
This section is described in Javascript-the definitive guide, 5th ed (O 'Reilly. In the case of global matching of regular expressions, the test () method maintains a lastindex attribute internally. Therefore, if we run the test twice in a method, the first result is correct, however, if lastindex is not set to zero for 2nd times, the remaining characters will be tested, and the comment above will be removed. If you run the two times, you will find that even if the input is correct, errors will always be displayed, confirm the description. This shows that the number of regular expressions in the function is still the original one. No new one is created, which leads to inconsistency in each test.
On the Internet, Google only found some fragmented information.Regular Expression traps in browsersThe possible problem is that the browser optimizes the direct amount of regular expressions, that is, the original direct amount object is reused instead of garbage collection. The simplest way to avoid this problem is to use less direct traffic and use new Regexp to generate it. In fact, the description in the ECMA-262 (highlighted below) should not have such a problem, each time from the direct amount to the regular expression object conversion should be different, even if they have the same content, in this case, is browser optimization outdated. Currently, this problem does not exist in IE. It exists in some very standard browsers.
A regular expression literal is an input element that is converted to a Regexp object (see 15.10) each time
Literal is evaluated. Two Regular Expression literals in a program evaluate to regular expression objects that
Never compare as ==== to each other even if the two literals 'contents are identical. A Regexp object may also
Be created at runtime by new Regexp (see 15.10.4) or calling the Regexp constructor as a function (15.10.3 ).