JavaScript match()Method

String 对象参考手册JavaScript String Object

Examples

Find "ain" in the string:

var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g);

nOutput the resulting array values:


Try it Yourself »

Definition and Usage

The match() method searches a string for a specified value, or finds one or more matches of a regular expression.

If you want to learn more about regular expression tutorials, please check out this site's:RegExp Tutorialand ourRegExp Object Reference Manual。

Note:The match() method searches a string object to find one or more texts that match regexp. The behavior of this method depends largely on whether regexp has the flag g. If regexp does not have the flag g, then the match() method can only perform one match in stringObject. If no matching text is found, match() will return null. Otherwise, it will return an array containing information about the matching text it found.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the match() method


Syntax

string.match(regexp)

Parameter Values

Parameter Description
regexp Required. Specifies a RegExp object of the pattern to be matched. If this parameter is not a RegExp object, it must first be passed to the RegExp constructor to convert it into a RegExp object.

Return Value

Type Description
Array An array storing the matching results. The content of this array depends on whether regexp has the global flag g. Returns null if no matching result is found.null 。

Technical Details

JavaScript Version: 1.2


More Examples

Example

Search for the string "ain" globally, case-insensitive:

var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/gi);

nOutput result:

ain,AIN,ain,ain

Try it Yourself »

Example

Check if it is a WeChat browser:

function is_weixn(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }

Try it Yourself »


String 对象参考手册JavaScript String Object

Other Extensions