lastMatch

The lastMatch property is a property of the RegExp object, and is used to return the last pattern matched. Consult the JavaScript Object Reference to learn how to use the RegExp object.

syntax:

RegExp.lastMatch

EXAMPLE

regExpSearch = new RegExp("the, to");

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

regExpArray = regExpSearch.exec(stringToSearch);

document.write("The last string matched is " + RegExp.lastmatch);

The example first starts with the creation of a new RegExp object, called regExpSearch. The search is for the word "the" and "to" (in the brackets). Next created is the string to search through, called stringToSearch. 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 lastMatch property of the RegExp object to return the last value , which is "to". The returned value looks like the following:

The last string matched is : to