title: Javascript Regex should pass .test() but appears to fail - why?
body: <p>I seem to be getting a consistently strange outcome when testing my regex in javascript.</p>
  
  <p>Here's my fiddle:
  <a href="http://jsfiddle.net/s5fYf/15/" rel="noreferrer">http://jsfiddle.net/s5fYf/15/</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Essentially the <code>/regex/.test(value)</code> 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!?</p>
  
  <p>I have tested my regex outside of the solution and as far as I can see it works.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Omitting the 'g' or global flag from my regex solved this issue.</p>
  
  <p>See the answer below and then this link for a full explanation of the global flag and its pitfalls:</p>
  
  <p><a href="https://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results">Why RegExp with global flag in Javascript give wrong results?</a></p>