Matt wrote:
Quote:
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]:
RegExp.prototype.test() works as designed, and W3Schools is more of a curse
than suitable reference material. Proof for both follows below, again.
Quote:
Input:
-----------------------------------------------
<html>
<body>
The test case is not even Valid markup to begin with.
<http://validator.w3.org/>
Quote:
<script type="text/javascript">
var patt = new RegExp('3','g');
One wonders why you did not use the more efficient RegExp initializer here,
supported since JavaScript 1.2 (Netscape 3.0), JScript 3.0 (MSHTML 4.0).
Maybe because of W3Schools babbling nonsense about how to "compile regexp"?
var patt = /3/g;
Quote:
document.write("Regex.test() inconsistencies: <br>")
for (var i=0; i<10; i++) {
if (patt.test("12345")==true) {
W3Schools nonsense as well: The method returns a boolean value already, so
omitting `==true' is equivalent and more efficient. That goes for almost
all comparisons with boolean values in almost all programming languages.
Quote:
document.write("pass<br>")
} else {
document.write("fail<br>")
}
And again. In fact, this can be rewritten much more efficient as
document.write((patt.test("12345") ? "pass": "fail") + "<br>");
Note the trailing semicolon which is indicative of good coding style, not
relying on automatic semicolon insertion and risking the side-effects
thereof. That isn't something you can find in W3Schools code, of course.
Quote:
}
>
</script>
</body>
</html>
>
Output:
-----------------------------------------------
Regex.test() inconsistencies:
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.
And then it did not occur to you to try applying Occam's Razor which would
mean that all the "major browsers" (whatever those might be) are correct and
only your reasoning is not?
Quote:
Seems like a bug to me, [...]
If only you had read the (ECMAScript) Specification(, Edition 3 Final)
before complaining:
| 15.10.6.2 RegExp.prototype.exec(string)
| [...]
|
| 15.10.6.3 RegExp.prototype.test(string)
|
| Equivalent to the expression RegExp.prototype.exec(string) != null.
The second call of RegExp.prototype.test() on that expression MUST return
`false' in a conforming implementation because there are no more matches for
the global-flagged regular expression, and RegExp.prototype.exec() would
therefore return `null'. That is also why the next call MUST return `true'
again as the matching index is reset and RegExp.prototype.exec() would not
return `null' (since there was a match), and why removing the `g' modifier
makes a difference.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee