After reading this post and this doc, I'm trying to write a function that will return the index of each occurrence of a regular expression in a string (in this case, each occurrence of a number). I took this the code from the documentation linked above:
var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) != null)
{
var msg = "Found " + myArray[0] + ". ";
msg += "Next match starts at " + myRe.lastIndex;
print(msg);
}
And turned it into this:
var myRe = /([0-9]*)/g;
var str = "gfarg h43kjh arjh 343";
var myArray;
while ((myArray = myRe.exec(str)) != null)
{
var msg = "Found " + myArray[0] + ". ";
msg += "Next match starts at " + myRe.lastIndex;
alert(msg);
}
Which would loop infinitely showing the same result. I actually have two questions. How can I show the index of each whole number (which in my sample string "gfarg h43kjh arjh 343"
would be 7 and 18). And, why is my current code looping infinitely?