Export (0) Print
Expand All

match Method (String) (JavaScript)

Matches a string with a regular expression, and returns an array containing the results of that search.

stringObj.match(rgExp) 

stringObj

Required. The String object or string literal on which to perform the search.

rgExp

Required. A regular expression object that contains the regular expression pattern and applicable flags. This can also be a variable name or string literal containing the regular expression pattern and flags.

If the match method does not find a match, it returns null. If it finds a match, match returns an array, and the properties of the global RegExp object are updated to reflect the results of the match.

If the global flag (g) is not set, Element zero of the array contains the entire match, while elements 1 through n contain any submatches. This behavior is the same as the behavior of the exec Method (Regular Expression) (JavaScript) when the global flag is not set. If the global flag is set, elements 0 through n contain all matches that occurred.

If the global flag is not set, the array returned by the match method has two properties, input and index. The input property contains the entire searched string. The index property contains the position of the matched substring within the complete searched string.

If the flag i is set, the search is not case-sensitive.

The following example illustrates the use of the match method.

var src = "azcafAJAC";

var re = /[a-c]/;

var result = src.match(re);

// The entire match is in array element 0.
document.write(result[0] + "<br/>");

// Now try the same match with the global flag.
var reg = /[a-c]/g;
result = src.match(reg);


// The matches are in elements 0 through n.
for (var index = 0; index < result.length; index++)
{
    document.write ("submatch " + index + ": " +  result[index]);
    document.write("<br />");
}

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Applies To: String Object (JavaScript)

Show:
© 2014 Microsoft