3

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?

Pablo
  • 28,133
  • 34
  • 125
  • 215

2 Answers2

4

This is a common behavior when you deal with patterns that have the global g flag, and you use the exec or test methods.

In this case the RegExp object will keep track of the lastIndex where a match was found, and then on subsequent matches it will start from that lastIndex instead of starting from 0.

Edit: In response to your comment, why doesn't the RegExp object 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

...

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.

....

You can make a simple proof by:

function createRe() {
  var re = /foo/g;
  return re;
}

createRe() === createRe(); // true, it's the same object

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.:

/foo/ === /foo/; // always false...

However this behavior is respected on all browser but not by IE, which initializes a new RegExp object every time.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Yea I was suspecting something like that, but how come those regex variables are not gc-ed and being reset? I believe each time I call parse() the new scope should be initialized. – Pablo May 11 '10 at 02:34
1

To avoid this behavior as it might be needed in this case, simply set

var r = /...pattern.../g;
var m = r.exec(html);
r.lastIndex=0;

This worked for me.

karora
  • 238
  • 1
  • 8