title: Javascript Regular expression test method no working properly
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <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>
  </blockquote>
  
  
  
  <p>I have the following method to which I'm passing these parameters:</p>
  
  <pre><code>var stringValue = "50, abc";
  var stringArray = stringValue.split(",");
  var agePattern = /^([0-9]|[1-9][0-9]|[1][0-4][0-9]|[1][5][0])$/g;
  
  age = getMatchingString(stringArray, agePattern);
  
  //---------------------------------------------
  
  function getMatchingString(stringArray, regexPattern) {
      //alert("getMatchingString");
      for (var i=0; i &lt; stringArray.length; i++) {
          if (regexPattern.test(stringArray[i])) {
  
              return (stringArray[i].match(regexPattern)).toString();
  
          }
      }
  
      return null;
  }
  </code></pre>
  
  <hr>
  
  <p>Chrome shows the following funny behavior where test method with stringArray[i] and stringArray[0] show different values even when <code>i = 0</code> as shown in the image:</p>
  
  <p><img src="https://i.stack.imgur.com/FgOtL.jpg" alt="Demonstration of the problem"></p>
  
  <p>Can someone explain this to me please?</p>