| exec() The exec() method is used to execute the search within the given string of text. It is a method of the RegExp object, a complete explanation of which can be found in the JavaScript Object Reference, RegExp. The results of the exec() method are returned in an array, which is normally given a name before the execution takes place, as you'll see in the example. The parameter of the exec() method is usually the name of the string that is to be searched. syntax: regExpName.exp(searchableStringName) 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". |