M
Matt
Hello all,
I have just discovered (the long way) that using a RegExp object with
the 'global' flag set produces inconsistent results when its test()
method is executed. I realize that 'global' is not an appropriate
modifier for the test() function - test() searches the entire string
by default.
However, I would expect it to degrade gracefully. Instead, I seem to
be getting something as follows - using W3Schools handy page at [1]:
Input:
-----------------------------------------------
<html>
<body>
<script type="text/javascript">
var patt = new RegExp('3','g');
document.write("Regex.test() inconsistencies: <br>")
for (var i=0; i<10; i++) {
if (patt.test("12345")==true) {
document.write("pass<br>")
} else {
document.write("fail<br>")
}
}
</script>
</body>
</html>
Output:
-----------------------------------------------
Regex.test() inconsistencies:
pass
fail
pass
fail
pass
fail
pass
fail
pass
fail
If you remove the 'g' modifier on the RegExp object, all of the tests
pass as you would expect. This happens on all current major browsers.
Seems like a bug to me, albeit with a fairly simply workaround. I
haven't found it reported anywhere else though - is my diagnosis
correct, and if so where does one go to report such things?
Thanks,
Matt.
[1] http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_compile_regexp
I have just discovered (the long way) that using a RegExp object with
the 'global' flag set produces inconsistent results when its test()
method is executed. I realize that 'global' is not an appropriate
modifier for the test() function - test() searches the entire string
by default.
However, I would expect it to degrade gracefully. Instead, I seem to
be getting something as follows - using W3Schools handy page at [1]:
Input:
-----------------------------------------------
<html>
<body>
<script type="text/javascript">
var patt = new RegExp('3','g');
document.write("Regex.test() inconsistencies: <br>")
for (var i=0; i<10; i++) {
if (patt.test("12345")==true) {
document.write("pass<br>")
} else {
document.write("fail<br>")
}
}
</script>
</body>
</html>
Output:
-----------------------------------------------
Regex.test() inconsistencies:
pass
fail
pass
fail
pass
fail
pass
fail
pass
fail
If you remove the 'g' modifier on the RegExp object, all of the tests
pass as you would expect. This happens on all current major browsers.
Seems like a bug to me, albeit with a fairly simply workaround. I
haven't found it reported anywhere else though - is my diagnosis
correct, and if so where does one go to report such things?
Thanks,
Matt.
[1] http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_compile_regexp