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 389263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:51:00+00:00 2026-05-12T15:51:00+00:00

What is the problem with this regular expression when I use the global flag

  • 0

What is the problem with this regular expression when I use the global flag and the case insensitive flag? Query is a user generated input. The result should be [true, true].

var query = 'Foo B';
var re = new RegExp(query, 'gi');
var result = [];
result.push(re.test('Foo Bar'));
result.push(re.test('Foo Bar'));
// result will be [true, false]

var reg = /^a$/g;
for(i = 0; i++ < 10;)
   console.log(reg.test("a"));
  • 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-05-12T15:51:00+00:00Added an answer on May 12, 2026 at 3:51 pm

    A RegExp object with the g flag keeps track of the lastIndex where a match occurred, so on subsequent matches it will start from the last used index, instead of 0. Take a look:

    var query = 'Foo B';
    var re = new RegExp(query, 'gi');
    console.log(re.lastIndex);
    
    console.log(re.test('Foo Bar'));
    console.log(re.lastIndex);
    
    console.log(re.test('Foo Bar'));
    console.log(re.lastIndex);

    If you don’t want to manually reset lastIndex to 0 after every test, just remove the g flag.

    Here’s the algorithm that the specs dictate (section 15.10.6.2):

    RegExp.prototype.exec(string)

    Performs
    a regular expression match of string
    against the regular expression and
    returns an Array object containing the
    results of the match, or null if the
    string did not match The string
    ToString(string) is searched for an
    occurrence of the regular expression
    pattern as follows:

    1. Let R be this RexExp object.
    2. Let S be the value of ToString(string).
    3. Let length be the length of S.
    4. Let lastIndex be the value of the lastIndex property on R.
    5. Let i be the value of ToInteger(lastIndex).
    6. If the global property is false, let i = 0.
    7. If i < 0 or i > length then set the lastIndex property of R to 0 and return null.
    8. Call [[Match]], giving it the arguments S and i. If [[Match]]
      returned failure, go to step 9;
      otherwise let r be its State result
      and go to step 10.
    9. Let i = i+1.
    10. Go to step 7.
    11. Let e be r’s endIndex value.
    12. If the global property is true, set the lastIndex property of R to e.
    13. Let n be the length of r’s captures array. (This is the same
      value as 15.10.2.1’s
      NCapturingParens.)
    14. Return a new array with the following properties:
    • The index
      property is set to the position of the
      matched substring within the complete
      string S.
    • The input property is set
      to S.
    • The length property is set to
      n + 1.
    • The 0 property is set to the
      matched substring (i.e. the portion of
      S between offset i inclusive and
      offset e exclusive).
    • For each
      integer i such that i > 0 and i ≤ n,
      set the property named ToString(i) to
      the ith element of r’s captures array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 303k
  • Answers 303k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Drupal is relatively secure in general, but vulnerabilities pop up… May 13, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer An old license lock was in affect when I use… May 13, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer The character type in FORTRAN is an unsigned 8 bit… May 13, 2026 at 8:32 pm

Related Questions

With my luck this question will be closed too quickly. I see a tremendous
I've isolated a problem with Ruby on Rails where a model with a serialized
I try to parse articles from wikipedia. I use the *page-articles.xml file, where they
Yesterday I tracked down a strange bug which caused a website display only a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.