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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:21:40+00:00 2026-05-28T18:21:40+00:00

Given this code: var reg = /a/g; console.log(reg.test(a)); console.log(reg.test(a)); I get this result: true

  • 0

Given this code:

var reg = /a/g;
console.log(reg.test("a"));
console.log(reg.test("a"));

I get this result:

true
false

I have no idea how this could happen. I have tested in both Node.js (v8) and Firefox browser.

  • 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-28T18:21:41+00:00Added an answer on May 28, 2026 at 6:21 pm

    To workaround the problem, you can remove the g flag or reset lastIndex as in

    var reg = /a/g;
    console.log(reg.test("a"));
    reg.lastIndex = 0;
    console.log(reg.test("a"));
    

    The problem arises because test is based around exec which looks for more matches after the first if passed the same string and the g flag is present.

    15.10.6.3 RegExp.prototype.test(string) # Ⓣ Ⓡ

    The following steps are taken:

    1. Let match be the result of evaluating the RegExp.prototype.exec (15.10.6.2) algorithm upon this RegExp object using string as the argument.
    2. If match is not null, then return true; else return false.

    The key part of exec is step 6 of 15.10.6.2:

    6. Let global be the result of calling the [[Get]] internal method of R with argument "global".
    7. If global is false, then let i = 0.

    When i is not reset to 0, then exec (and therefore test) does not start looking at the beginning of the string.

    This is useful for exec because you can loop to handle each match:

     var myRegex = /o/g;
     var myString = "fooo";
     for (var match; match = myRegex.exec(myString);) {
       alert(match + " at " + myRegex.lastIndex);
     }
    

    but obviously it isn’t so useful for test.

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

Sidebar

Related Questions

I have following code snippet: var reg = /^[0-9]$/; $('input[type=text]').keypress(function(){ console.log(reg.test(parseInt($(this).val()))); }); When I
If I run this code - var html= '<html><head></head><body><div class=bar></div></body></html>'; console.log($(html).find('div')); I get no
This code slickgrid.onDblClick.subscribe(function(e){ var cell = slickgrid.getCellFromEvent(e); console.log(cell.row, cell.cell); }); prints 0, 0 if
I have some troubles with this small JavaScript code: var text=Z Test Yeah !
Given this code : counter = 0; $('div').each(function () { counter++; console.log(counter + ':
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this code: var minX = minY = maxX = maxY = 0; for(var
I have this code: function makeUser() { $info['cn'] = Test User; $info['sn'] = User;
Given this code: var arr = []; for (var i = 0; i <
Given this code: using (var conn = new SqlConnection(...)) { conn.Open(); using (var cmd

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.