![]() |
regex test problem
How do you explain that ?
Code:
regexPattern = new RegExp("a","gi");Code:
alert (regexPattern.test(s) + s.match(regexPattern));Code:
s.match(regexPattern) |
Hmm. Very odd! You seem to have exposed a bug (not a flower! :D) in the Javascript regex engine. Same in IE and Chrome.
Code:
var s = "a";"If you make it idiot proof, they'll build a better idiot" |
Quote:
Yeah, tested the thing in chrome, before submiting here. Not to mention hours lost, before 'lamp' turned on: "Damn, there something smell bad about .test". I had .test in a loop and "a" string has been changing and on every second loop bad result. |
Quote:
Code:
regexPattern = new RegExp("a","gi"); |
If you remove the "g" global modifier - which is not relevant to test() - then it behaves as expected.
Added: I won't have JavaScript besmirched :D (only joshing!) |
Quote:
We learn something every day. Sometimes something worth knowing! |
It is the same thing with prompt or confirm and if an "a" take place of s !
In short (See this MDN page *) you have to restore the regexPattern.lastIndex to 0 before each new test ! Good to know. Thanks ! (*) Or better ECMAScript Language Specification 15.10.6.2 and 15.10.6.3 |
| All times are GMT +1. The time now is 11:34 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.