lastIndex

The lastIndex property is a property of the RegExp object, and is used to return the value of the next index within the array constructed by the RegExp object upon encountering matches to the given search string or strings. For a detailed explanation of the RegExp object and its uses, see the JavaScript Object Reference, RegExp.

syntax:

RegExp.lastMatch

EXAMPLE

regExpSearch = new RegExp("th");

stringToSearch = "This is the string to be searched.";

regExpArray = regExpSearch.exec(stringToSearch);

document.write("The next value in the array is : " +RegExp.lastIndex);

The example first starts with the creation of a new RegExp object, called regExpSearch. The search is for the letters "th". Next created is the string to search through, called stringToSearch, with the letters "th" in brackets. Next comes another variable which contains the RegExp expression (regExpSearch) and the exec() method, which performs the actual execution of the search. Contained as a condition within the exec() method is the variable representing the string to be searched, stringToSearch. Finally a document.write statement is declared, which uses the lastIndex property of the RegExp object to return the next value in the array, which is "this". The returned value looks like the following:

The next value in the array is : 10