Possible Duplicate:
Why RegExp with global flag in Javascript give wrong results?
I have the following method to which I'm passing these parameters:
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 < stringArray.length; i++) {
if (regexPattern.test(stringArray[i])) {
return (stringArray[i].match(regexPattern)).toString();
}
}
return null;
}
Chrome shows the following funny behavior where test method with stringArray[i] and stringArray[0] show different values even when i = 0
as shown in the image:
Can someone explain this to me please?