Gary Wardell wrote:
Should it alternately between displaying "Match" and "Null" each time it's
called?
I would think that since the regex and the string are always the same if
should always display "Match"???
What I am seeing is that it alternates.
If this is proper behavior, why?
Use
function test()
{
var re;
re = new RegExp('^(.+)\\@(.+?)$', 'gi');
if (re.exec("te**@example.com") != null)
{
alert("Match");
} else {
alert("Null");
}
return;
}
or live with the ECMAScript specification insisting on creating one
regular expression object for each regular expression literal, even if
it is inside of a function as a local variable:
"7.8.5 Regular Expression Literals
A regular expression literal is an input element that is converted to a
RegExp object (section 15.10) when it is
scanned. The object is created before evaluation of the containing
program or function begins. Evaluation of the
literal produces a reference to that object; it does not create a new
object."
So what happens is that in your code the regular expression literal
creates one regular expression object with the g flag when the program
code is scanned, and then when the function is executed that regular
expression object is referenced and because of the g flag every second
exec starts after the last match and does not find anything.
--
Martin Honnen
http://JavaScript.FAQTs.com/