I'm having trouble interpreting the following code from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while ((myArray = myRe.exec(str)) !== null) { //confused by this line
var msg = 'Found ' + myArray[0] + '. ';
msg += 'Next match starts at ' + myRe.lastIndex;
console.log(msg);
}
I'm confused by the while loop line. It seems to set myArray to the result of calling exec on the string str and says to continue if its not equal to null.
What I don't get is how the program knows to start searching from the last index where a match was found. To me it looks like this should be an infinite loop b/c I can't see where it says to start searching from the next index in an array.
Also, is there any reason to use exec to find multiple matches rather than just using match? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match