If your regular expression uses the g flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property. For example, assume you have this script: |
<SCRIPT LANGUAGE="JavaScript1.2"> |
myRe=/ab*/g; |
str = "abbcdefabh" |
myArray = myRe.exec(str); |
document.writeln("Found " + myArray[0] + ". Next match starts at " + myRe.lastIndex) |
mySecondArray = myRe.exec(str); |
document.writeln("Found " + mySecondArray[0] + ". Next match starts at " + myRe.lastIndex) |
</SCRIPT> |
This script displays the following text: |
Found abb. Next match starts at 3 |
Found ab. Next match starts at 9 |