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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:50:32+00:00 2026-05-29T16:50:32+00:00

I seem to be getting a consistently strange outcome when testing my regex in

  • 0

I seem to be getting a consistently strange outcome when testing my regex in javascript.

Here’s my fiddle:
http://jsfiddle.net/s5fYf/15/

This is taken from a web project I’m building. I pass an array of validation objects into my validation function which iterates through them, validating each rule against the value. If one is false it should stop the loop and return a return object which picks up a message and cssClass from the failed rule.

The problem is the validation method seems to return false even if the regex test passes, which should be impossible! So I feel like I’m missing something key. From the debug output you can see that the regex test that is output passes, but then obviously fails when it is tested in the code. This is inline with what I see in my project where if I omit the debug output the return value basically toggles between true and false.

Essentially the /regex/.test(value) function seems to oscillate between true and false which is consistent but not what I was expecting… So, my question is what is causing this bizarre behaviour!?

I have tested my regex outside of the solution and as far as I can see it works.

UPDATE:

Omitting the ‘g’ or global flag from my regex solved this issue.

See the answer below and then this link for a full explanation of the global flag and its pitfalls:

Why RegExp with global flag in Javascript give wrong results?

  • 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-29T16:50:44+00:00Added an answer on May 29, 2026 at 4:50 pm

    It boils down to the fact that the test method of javascript regular expressions returns a result and moves a pointer on to after the match.

    So the first call returns true, then the second returns false. Check this example: http://jsfiddle.net/B9aVA/

    var regex = /^.+$/g
    var input = "Hello";
    console.log(regex.test(input));
    console.log(regex.test(input));
    

    Writes

    true
    false
    

    So your code which calls test twice:

    case "regex":
        $(".debug-info").append('<span>Result: ' + validation[i].rule.test(value) + '</span><br />');
         if (!validation[i].rule.test(value))
              returnValue.isValid = false;
         break;
    

    My suggestion is to call test once and store the result in a variable, and use that instead

    case "regex":
        var result = validation[i].rule.test(value);
        $(".debug-info").append('<span>Result: ' + result + '</span><br />');
         if (!result)
              returnValue.isValid = false;
         break;
    

    Moral of the story: Methods can have side effects, that’s what distinguishes them from properties.

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

Sidebar

Related Questions

I seem to be getting a strange error when I run my tests in
I seem to be getting this particular always when i am trying to receive
I've been coding for a while now but seem to be getting this issue
I seem to be getting negative values for object_id for some objects. Is this
been working on this for two days now and seem to be getting nowhere.
I seem to be getting this error... I believe I am typing everything correctly:
i seem to be getting this error everytime i try to run my android
I seem to be getting a strange situation where CodeSniffer is applying 2 indent
I seem to be getting an error when testing on my WinXP(SP3) PC. The
I am getting this error in my VB.NET code. Anyone have ideas on what

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.