JavaScript » RegExp » lastIndex

This property is an integer that specifies the index at which to start the next match, but is only set if the regular expression uses the 'g' flag to specify a global search.

It consists of an integer specifying the index (counting from the beginning of the string and including all alphanumeric and non-alphanumeric characters) of the first character after the last match.

Examples

Code:
myRexp = /ships*\s/g
myRexp.exec("the hardships of traveling")
document.write(myRexp.lastIndex)
Output:
14
Explanation:

If the value of the lastIndex property is greater than the length of the string, then both the test and exec methods will fail, and the lastIndex property will be set to 0. For example, this code will match the final 'ing' of 'traveling' and set the lastIndex property to 26 (one more than the index of the last character of the string). If you then immediately run the match again, it will fail and the lastIndex property will be set to 0.

Code:
myRexp = /ing/g
myRexp.exec("the hardships of traveling")
Explanation:

This example will match the final 'ing' of 'traveling' and set the lastIndex property to 26 (one more than the index of the last character of the string). If you then immediately run the match again, it will fail and the lastIndex property will be set to 0.