match()
is the same as RegExp
object's exec()
method.
The match()
method accepts a
regular-expression string or a RegExp
object.
The first item of the returning array is the string that matches the entire pattern. Each other item of the returning array represents capturing groups in the expression.
This method returns null if no match is found.
match() |
Yes | Yes | Yes | Yes | Yes |
stringObject.match(regexp);
Parameter | Description |
---|---|
regexp | Required. The value to search for as a regular expression. |
An array containing the matches, one item for each match.
It returns null if no match is found.
var text = "look, room, doom";
var pattern = /.oo/;
var matches = text.match(pattern);
console.log(matches.index); //0
console.log(matches[0]); //"oo"
console.log(pattern.lastIndex); //0
The code above generates the following result.