Evertjan. said:
Michael Winter wrote on 30 okt 2006 in comp.lang.javascript:
Evertjan. wrote:
[snip]
Even more so, setting the global flag in a test() never makes any
sense.
Never? I don't know about that. Rare, certainly.
Never!
Care to state a reason?
The information that can be gleaned from using the RegExp.prototype.test
method in this way is limited[1], which is why such usage would be rare.
However, that is a far cry from claiming that it makes no sense. Indeed,
my previous post demonstrated a reasonable use.
The point of the global flag is to allow repetitive processing, where
the lastIndex property indicates the position from which the next
invocation starts. This would allow the test method to assert that there
is more than one match, or even that one begins after a certain point
should the lastIndex property be set explicitly. If that's all that's
required, then there's no need to use a method that would return more
information (and be wasteful, in the process).
For example, one way to count the number of occurrences of a pattern
within a string is to use the String.prototype.match method[1]:
var result = string.match(regExp),
count = result ? result.length : 0;
Michael, I said: in "test()"
I read what you wrote.
"match()" is not "test()"
If you read past the part that you quoted, you would notice that I go on
to present an equivalent using the test method and a regular expression
with the global flag set. Mentioning the String.prototype.match method
was merely a comparison.
I wrote a little more than that.
Again, would you like to actually provide an explanation?
The test method is /defined/ in terms of the exec method; it is the
behavioural equivalent of:
regExp.exec(string) == null
/including/ all of the side effects that the exec method introduces. The
method should be used with that in mind, and if it's not, then it's the
fault of the developer and nobody else.
Note that an implementation doesn't have to use that exact expression.
Instead, it might copy the algorithm of the exec method (see 15.10.6.2),
except returning false instead of null in step 6, and returning true
instead of steps 12 and 13. This would save some time whilst providing
the same behaviour, however this latter issue is the most significant.
The subject of this thread is:
"regexp test function behavior"
not:
"regexp match function behavior"
I know. I answered the OP's question, did I not? Even so, threads drift.
"match()" is not "test()"
I hope you're going to feel a little silly now after banging on about
that so irrationally.
[1] Browsers return null from the RegExp.prototype.match method
[snip]
"match()" is not "test()"
That comment was an aside, which was why I presented it as an endnote.
Mike
[1] As far as I can see, only three facts can be obtained:
1. Whether the string matched the pattern (the return value
of the method itself),
2. The location of first character to follow the match just
obtained (the value of the lastIndex property), and
3. Whether the pattern matched a zero-length string (the
lastIndex property will not have changed).