[EMAIL PROTECTED] wrote:
> There's some odd behavior with the RegExp "test" method. With the
> global flag, calls to test which should return true actually
> alternately return true and false:
> 
> js> r = RegExp("asdf", "g");
> /asdf/g
> js> r.test("asdf")
> true
> js> r.test("asdf")
> false
> js> r.test("asdf")
> true
> js> r.test("asdf")
> false
> ...
> 
> ("Rhino 1.6 release 7 2007 08 19")
> 
> Without the global flag it always works correctly, and if test should
> return false it always does.
> 
> Is this a bug? I don't know of any other regex implementations with
> this behavior. I don't think there's any reason to set the global flag
> when using test(), but I also don't think it should break it.

Which other implementations are you talking about? Other ECMAScript 
implementations like Spidermonkey? I think they should return the same 
result.
The specification defines the test method as follows:

"15.10.6.3 RegExp.prototype.test(string)
Equivalent to the expression RegExp.prototype.exec(string) != null."

With the global flag being set the exec call sets the lastIndex property 
of the regular expression object and that way the lastIndex after the 
first (successful) test becomes 4 meaning the second test starts at the 
end of the string and that way does not find a match. That way the 
lastIndex is reset to 0 meaning the third test finds a match again and 
so on.

-- 

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to