Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:29:57+00:00 2026-06-07T12:29:57+00:00

In the Chrome or Firebug console: reg = /ab/g str = abc reg.exec(str) ==>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:29:59+00:00Added an answer on June 7, 2026 at 12:29 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I run this in a JavaScript console in Chrome or Firebug, it works
I get this behavior in both Chrome (Developer Tools) and Firefox (Firebug). Note the
Working in Firebug Console, but not from file. Google Chrome - Uncaught TypeError: Object
I've checked this in firebug on Chrome and FF. No errors are reported and
in firefox I can usee firebug, in chrome I can use the css console.
I do a lot of my work from Chrome's developer tools console and firebug
If you open this JSFiddle , you should see in Firebug/Chrome Dev Tools that
Whenever I use Console in Chrome's Developer Tools or Firebug to interpret a jQuery
how do i use firebug or chrome console instead of alert() to see variable
using firebug console in firefox for example when execute this script $(body).css(border,4px solid red);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.