JavaScript's String.match and RegExp.exec Methods
JavaScript's string match
and regular expression exec
methods are both used to search strings. They both provide more detailed information about matches than the more basic string search methods. The exec
method is the more powerful of the two. We describe and demonstrate both methods on this page.
String match Method
Invoke the string match
method on the string you want to search, and pass the pattern to search for as the only argument. The match
method expects a regular expression argument, but you can pass a string and it will convert it to a regular expression for you.
The match
method returns an array if a match is found; it returns null
if no match is found. The returned array differs depending upon whether the argument passed to the method includes the global flag.
Not Global: If the argument to the match
method does not include the global modifier, the search looks for a single match in the string. The returned array holds that match as its first element. Subsequent array elements hold matches to parenthesized sub-expressions of the regular expression argument. The index
property of the returned array holds the location of the match in the string searched. The input
property of the array holds the string searched. An example demonstrates:
var str = 'My phone number is: 555 123-4567. My cell number is: 555 987-5432.';
var re = /(\d{3})\D(\d{3})-(\d{4})/;
var ar = str.match(re);
console.log( ar ); // [ "555 123-4567", "555", "123", "4567" ]
console.log( ar.index ); // 20
Global: If the regular expression argument to the match
method includes the global flag, the returned array elements hold the matches. There is no means to access the location of the matches in the original string, nor is there a way to capture parenthesized sub-expressions.
var str = 'My phone number is: 555 123-4567. My cell number is: 555 987-5432.';
var re = /\d{3}\D\d{3}-\d{4}/g;
var ar = str.match(re);
console.log( ar ); // [ "555 123-4567", "555 987-5432" ]
console.log( ar.index ); // undefined
The regular expression exec
method gives access to more information for global searches.
RegExp exec Method
The exec
method is invoked on a regular expression and takes a string to search as its only argument. When the regular expression does not have the global modifier set, the results for exec
are the same as those for the string match
method without the global modifier. (See the first example above.)
When the global modifier is set, the exec
method is run in a loop to access full information about the matches. An example demonstrates:
var str = 'My phone number is: 555 123-4567. My cell number is: 555 987-5432.';
var re = /(\d{3})\D(\d{3})-(\d{4})/g;
var ar;
while ( (ar = re.exec(str)) !== null ) {
console.log( ar[0] );
console.log( ar );
console.log( ar.index );
console.log( re.lastIndex );
}
/* console.log output:
(first loop)
555 123-4567
[ "555 123-4567", "555", "123", "4567" ]
20
32
(second loop)
555 987-5432
[ "555 987-5432", "555", "987", "5432" ]
53
65
*/
The while loop continues to run until exec
returns null
. Since our string has two matches, the loop runs twice. The array returned in each iteration contains the match in its first element, and matches for parenthesized sub-expressions in subsequent elements. The index
property of the returned array for each iteration holds the location of the match.
The lastIndex
property of the regular expression holds the location of the last character of the last match; this is where the search through the string resumes in the next iteration. When no more matches are found, the lastIndex
property is reset to zero.