Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

16
  • 24
    This is what I was hoping for. What I've never seen in JavaScript documentation is mention that the exec() method will continue to return the next result set if called more than once. Thanks again for the great tip! Commented Feb 6, 2009 at 16:44
  • 1
    It does because of this: regular-expressions.info/javascript.html (Read through: "How to Use The JavaScript RegExp Object") Commented Feb 6, 2009 at 16:54
  • 1
    there is a bug in this code: the semicolon after the "while" should be removed. Commented Feb 23, 2010 at 16:31
  • 1
    Because I generally only use normal (i.e. capturing) groups if I'm actually interested in their content. Commented Dec 4, 2013 at 13:13
  • 1
    @KnightYoshi Yes. In JavaScript any expression also produces its own result (like x = y would assign y to x and also produce y). When we apply that knowledge to if (match = re.exec(url)): This A) does the assignment and B) returns the result of re.exec(url) to the while. Now re.exec returns null if there is no match, which is a falsy value. So in effect the loop will continue as long as there is a match. Commented Sep 24, 2014 at 12:34