Javascript Reference - JavaScript String match() Method








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.

Browser Support

match() Yes Yes Yes Yes Yes

Syntax

stringObject.match(regexp);




Parameter Values

Parameter Description
regexp Required. The value to search for as a regular expression.

Return Value

An array containing the matches, one item for each match.

It returns null if no match is found.

Example


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.