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

  • Home
  • SEARCH
  • 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 8933403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:31:44+00:00 2026-06-15T09:31:44+00:00

I have made a simple code for capturing a certain group in a string

  • 0

I have made a simple code for capturing a certain group in a string :

/[a-z]+([0-9]+)[a-z]+/gi    (n chars , m digts , k chars).

code :

var myString='aaa111bbb222ccc333ddd';
var myRegexp=/[a-z]+([0-9]+)[a-z]+/gi;

var match=myRegexp.exec(myString);
console.log(match)
 
 while (match != null)
{
  match = myRegexp.exec(myString);
  console.log(match)
}

The result were :

["aaa111bbb", "111"]
["ccc333ddd", "333"]
null

But wait a minute ,
Why he didnt try the bbb222ccc part ?

I mean ,
It saw the aaa111bbb but then he should have try the bbb222ccc… ( That’s greedy !)

What am I missing ?

Also

looking at

   while (match != null)
    {
      match = myRegexp.exec(myString);
      console.log(match)
    }

how did it progressed to the second result ?
at first there was :

var match=myRegexp.exec(myString);

later ( in a while loop)

match=myRegexp.exec(myString);
match=myRegexp.exec(myString);

it is the same line … where does it remember that the first result was already shown ?

  • 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-15T09:31:45+00:00Added an answer on June 15, 2026 at 9:31 am

    .exec is stateful when you use the g flag. The state is kept in the regex object’s .lastIndex property.

    var myString = 'aaa111bbb222ccc333ddd';
    var myRegexp = /[a-z]+([0-9]+)[a-z]+/gi;
    var match = myRegexp.exec(myString);
    console.log(myRegexp.lastIndex); //9, so the next `.exec` will only look after index 9
    while (match != null) {
        match = myRegexp.exec(myString);
        console.log(myRegexp.lastIndex);
    }
    

    The state can be resetted by setting .lastIndex to 0 or by execing a different string. re.exec("") for instance will reset the state because the state was kept for 'aaa111bbb222ccc333ddd'.

    The same applies to .test method as well, so never use g flag with a regex that is used for .test if you prefer no surprises. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec

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

Sidebar

Related Questions

I have made a simple code for ajax to call a page but it
Beginner in Android development. My code crashes. I have made a simple Java method
I have made a simple server -client code in python socket programming. Client side
I have some relatively simple code I have made that is supposed to retrieve
i have made a simple login code for my program. On my website i
I have made a Simple CUDA dll the code which I am displaying below.
I have made simple encryption/decryption method in php that I'm trying to move to
OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
I have made a simple chat application in PHP/Ajax/MySQL. I am regularly calling these
I have made a simple Line Chart: Which is showing 4 grid-lines at points

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.