1 | 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: |
2 | <SCRIPT LANGUAGE="JavaScript1.2"> |
3 | myRe=/ab*/g; |
4 | str = "abbcdefabh" |
5 | myArray = myRe.exec(str); |
6 | document.writeln("Found " + myArray[0] + ". Next match starts at " + myRe.lastIndex) |
7 | mySecondArray = myRe.exec(str); |
8 | document.writeln("Found " + mySecondArray[0] + ". Next match starts at " + myRe.lastIndex) |
9 | </SCRIPT> |
10 | This script displays the following text: |
11 | Found abb. Next match starts at 3 |
12 | Found ab. Next match starts at 9 |