have funciton in my object which is called regularly.
parse : function(html)
{
var regexp = /...some pattern.../
var match = regexp.exec(html);
while (match != null)
{
...
match = regexp.exec(html);
}
...
var r = /...pattern.../g;
var m = r.exec(html);
}
with unchanged html the m returns null each other call. let’s say
parse(html);// ok
parse(html);// m is null!!!
parse(html);// ok
parse(html);// m is null!!!
// ...and so on...
is there any index or somrthing that has to be reset on html … I’m really confused. Why match always returns proper result?
This is a common behavior when you deal with patterns that have the global
gflag, and you use theexecortestmethods.In this case the
RegExpobject will keep track of thelastIndexwhere a match was found, and then on subsequent matches it will start from thatlastIndexinstead of starting from 0.Edit: In response to your comment, why doesn’t the
RegExpobject being re-created when you call the function again:This is the behavior described for regular expression literals, let me quote the specification:
§ 7.8.5 – Regular Expression Literals
You can make a simple proof by:
You can be sure that is the same object, because “two regular expression literals in a program evaluate to regular expression objects that never compare as
===to each other even if the two literals’ contents are identical”, e.g.:However this behavior is respected on all browser but not by IE, which initializes a new RegExp object every time.