JavaScript tutorial:
match method

 

Applies to: String Object

The match method returns, as an array, the results of a search on a string using a supplied Regular Expression object.

Syntax

stringObj.match(rgExp)

The match method syntax has these parts:

Part

Description

stringObj

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

rgExp

Required. The regular expression to use in the search.

Return value

The match method returns, as an array, the results of a search on a string using a supplied Regular Expression object.

Example

The match method, which behaves like the exec method, returns an array of values. Element zero of the array contains the last matched characters. Elements 1...n contain matches to any parenthesized substrings in the regular expression.

The method updates the contents of the RegExp object.

The following example illustrates the use of the match method:

function contain(str1,str2)
{
var r, re;
var s = "The quick brown fox jumped over the lazy yellow dog.";
re = new RegExp(str2)
r = s.match(re);
if(r==null)
return false;
else
return true;
}
document.write(contain("The quick brown fox jumped over the lazy yellow dog.","fox"));

  To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: exec method, RegExp Object, replace method, search method, String Object methods, test method