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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:50:15+00:00 2026-05-10T17:50:15+00:00

I have code similar to this filtering entries in an Array of Objects: var

  • 0

I have code similar to this filtering entries in an Array of Objects:

var filterRegex = new RegExp('.*blah.*','ig'); if (filterRegex.test(events[i].thing) && events[i].show) {     console.log('SUCCESS: filtering thing ' + i + ' ' + events[i].thing);     events[i].show = false;     numevents--; } 

I get inconsistent results with this if condition (checking with Firebug, both conditions are true individually, but sometimes the whole expression evaluates to false). HOWEVER, if I actually put an alert() called inside this if statement (like line 4), it becomes consistent and I get the result I want.

Can you see anything wrong with this logic and tell me why it’s not always producing what is expected?

  • 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. 2026-05-10T17:50:15+00:00Added an answer on May 10, 2026 at 5:50 pm

    Ok, i see it now. The key to your problem is the use of the g (global match) flag: when this is specified for a regex, it will be set up such that it can be executed multiple times, beginning each time at the place where it left off last time. It keeps a ‘bookmark’ of sorts in its lastIndex property:

    var testRegex = /blah/ig; // logs: true 4 console.log(testRegex.test('blah blah'), testRegex.lastIndex); // logs: true 9  console.log(testRegex.test('blah blah'), testRegex.lastIndex); // logs: false 0 console.log(testRegex.test('blah blah'), testRegex.lastIndex); 

    The above example creates an instance of a very simple regex: it matches ‘blah’, upper or lower case, anywhere in the string, and it can be matched multiple times (the g flag). On the first run, it matches the first ‘blah’, and leaves lastIndex set to 4 (the index of the space after the first ‘blah’). The second run starts matching at the lastIndex, matches the second blah, and leaves lastIndex set to 9 – one past the end of the array. The third run doesn’t match – lastIndex is bogus – and leaves lastIndex set to 0. A fourth run would therefore have the same results as the first.

    Now, your expression is quite a bit more greedy than mine: it will match any number of any characters before or after ‘blah’. Therefore, no matter what string you test on, if it contains ‘blah’ it will always match the entire string and leave lastIndex set to the length of the string just tested. Meaning, if you were to call test() twice, the second test would always fail:

    var filterRegex = /.*blah.*/ig; // logs: true, 9 console.log(filterRegex.test('blah blah'), filterRegex.lastIndex); // logs: false, 0  console.log(filterRegex.test('blah blah'), filterRegex.lastIndex); 

    Fortunately, since you create your regex immediately prior to calling test(), and never call test() more than once, you’ll never run into unexpected behavior… Unless you’re using a debugger that lets you add in another call to test() on the side. Yup. With Firebug running, a watch expression containing your call to test() will result in intermittent false results showing up, either in your code or in the watch results, depending on which one gets to it first. Driving you slowly insane…

    Of course, without the g flag, livin’ is easy:

    var filterRegex = /.*blah.*/i; // logs: true, 0 console.log(filterRegex.test('blah blah'), filterRegex.lastIndex); // logs: true, 0  console.log(filterRegex.test('blah blah'), filterRegex.lastIndex); 

    Suggestions

    • Avoid the global flag when you don’t need it.
    • Be careful what you evaluate in the debugger: if there are side effects, it can affect the behavior of your program.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer MyGeneration is open source, is template driven, and the templates… May 11, 2026 at 2:46 pm
  • added an answer First install a regular expression function. This article has code… May 11, 2026 at 2:46 pm
  • added an answer Fortunately there's a way to force IE7 to use the… May 11, 2026 at 2:46 pm

Related Questions

The GString concept in Groovy is pretty powerful (see http://groovy.codehaus.org/Strings+and+GString ). GStrings let you
Where I'm at we have a software package running on a mainframe system. The
I have code similar to this in my application: class A { public: int
I have code similar to this: class Foo { List<Bar> _myList; ... public IEnumerable<Bar>

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.