163

In the Chrome or Firebug console:

reg = /ab/g
str = "abc"
reg.exec(str)
   ==> ["ab"]
reg.exec(str)
   ==> null
reg.exec(str)
   ==> ["ab"]
reg.exec(str)
   ==> null

Is exec somehow stateful and depends on what it returned the previous time? Or is this just a bug? I can't get it to happen all the time. For example, if 'str' above were "abc abc" it doesn't happen.

0

3 Answers 3

291

A JavaScript RegExp object is stateful.

When the regex is global, if you call a method on the same regex object, it will start from the index past the end of the last match.

When no more matches are found, the index is reset to 0 automatically.


To reset it manually, set the lastIndex property.

reg.lastIndex = 0;

This can be a very useful feature. You can start the evaluation at any point in the string if desired, or if in a loop, you can stop it after a desired number of matches.


Here's a demonstration of a typical approach to using the regex in a loop. It takes advantage of the fact that exec returns null when there are no more matches by performing the assignment as the loop condition.

var re = /foo_(\d+)/g,
    str = "text foo_123 more text foo_456 foo_789 end text",
    match,
    results = [];

while (match = re.exec(str))
    results.push(+match[1]);

DEMO: http://jsfiddle.net/pPW8Y/


If you don't like the placement of the assignment, the loop can be reworked, like this for example...

var re = /foo_(\d+)/g,
    str = "text foo_123 more text foo_456 foo_789 end text",
    match,
    results = [];

do {
    match = re.exec(str);
    if (match)
        results.push(+match[1]);
} while (match);

DEMO: http://jsfiddle.net/pPW8Y/1/

Sign up to request clarification or add additional context in comments.

12 Comments

You can also reset it with reg.exec("") because it only retains the state if you pass the same string
It might be a good thing to include a while loop example, as this demonstrates the usefulness of this particular feature.
@Esailija @squint that's not entirely true. If you use the same regex to match against different strings, then the lastIndex remains set to where it was in the first string. I ran into this problem today.
...oh, are you talking about Esailija's "only retains state if you pass the same string" comment? Yeah, that's not right. His technique resets it because it fails to find a match in an empty string. Maybe there's some case where it doesn't reset? I wouldn't use that technique anyway.
In case anyone has read this far, here's a fiddle to further explain my plight (which is explained by @squint's answer above): jsfiddle.net/grammar/k2hxsq8d/2
|
37

From MDN docs:

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test will also advance the lastIndex property).

Since you are using the g flag, exec continues from the last matched string until it gets to the end (returns null), then starts over.


Personally, I prefer to go the other way around with str.match(reg)

Comments

14

Multiple Matches

If your regex need the g flag (global match), you will need to reset the index (position of the last match) by using the lastIndex property.

reg.lastIndex = 0;

This is due to the fact that exec() will stop on each occurence so you can run again on the remaining part. This behavior also exists with test()) :

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test will also advance the lastIndex property)

Single Match

When there is only one possible match, you can simply rewrite you regex by omitting the g flag, as the index will be automatically reset to 0.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.