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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:37:47+00:00 2026-06-15T13:37:47+00:00

Possible Duplicate: Why RegExp with global flag in Javascript give wrong results? My code

  • 0

Possible Duplicate:
Why RegExp with global flag in Javascript give wrong results?

My code is as follows,

HTML:

<p id="test"></p>​

JavaScript:

var patt = /h/gi;
var arr = ["", "2Hour", "4Hour", "8Hour", "Next Business Day"];
var test = document.getElementById("test");

for (var i = 0; i < arr.length; i++)
{
    if (patt.test(arr[i])) {
        test.innerHTML += " " + arr[i];
    }
}

However, the output that get is 2Hour 8Hour, why’s 4Hour not a part of the output?

Is there something wrong with my regex? how can I solve this issue?

I’ve put it up on fiddle

​

  • 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-15T13:37:48+00:00Added an answer on June 15, 2026 at 1:37 pm

    To quote MDN:

    As with exec (or in combination with it), test called multiple times
    on the same global regular expression instance will advance past the
    previous match.

    Currently what is happening is:

    1. the regular expression stored in patt tests the second string, finds a match at the index 1, and retains this information
    2. The next time you use test it tries to find a match from index 1
      onwards, which obviously yields no match, since it is effectively testing the string "our"
    3. This resets the pointer to 0, allowing the match in the next
      string at index 1 to be found.

    So to solve your problem, simply create a new instance on each iteration:

    for (var i = 0; i < arr.length; i++)
    {
        if (/h/gi.test(arr[i])) {
            test.innerHTML += " " + arr[i];
        }
    }
    

    This “clears the pointer” as it were, and ensures that the regex behaves identically for each string it is tested against.

    Here is a demonstration: http://jsfiddle.net/QbXEX/12/

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

Sidebar

Related Questions

Possible Duplicate: Why RegExp with global flag in Javascript give wrong results? var reg1
Possible Duplicate: Interesting test of Javascript RegExp Regular expression test can't decide between true
Possible Duplicate: javascript new regexp from string I have an regex test: // contributed
Possible Duplicate: Decode obfuscated JavaScript Question: I have the following javascript code: var _0xe91d=[\x28\x35\x28\x24\x29\x7B\x24\x2E\x32\x77\x2E
Possible Duplicate: php regexp: remove all attributes from an html tag $input = '<div
Possible Duplicate: Javascript regex returning true.. then false.. then true.. etc var r =
Possible Duplicate: Javascript Regexp dynamic generation? I am trying to create a new RegExp
Possible Duplicate: JavaScript RegExp Three Parts I need a help with regular expressions in
Possible Duplicate: Converting user input string to regular expression javascript new regexp from string
Possible Duplicate: Validate email address in Javascript? Can anyone help me with email validation

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.