Latest web development tutorials

JavaScript match () method

String Object Reference JavaScript String Object

Examples

Find the string "ain":

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

n output the resulting value array:


try it"

Definition and Usage

match () method to retrieve the value specified in the string, or find one or more regular expression matching.

If you want to know more please see the regular expression tutorial site: RegExp tutorials and our RegExp Object Reference Manual .

Note: match () method to retrieve a string String Object, to find one or more matching regexp text. The behavior of this method depends largely on whether regexp has a flag g. If regexp no sign g, the match () method can only be executed once the match stringObject. If no matches are found in the text, match () will return null. Otherwise, it returns an array that holds the information and matching text it finds relevant.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support match () method


grammar

string.match( regexp )

Parameter Value

参数 描述
regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。

return value

类型 描述
Array 存放匹配结果的数组。该数组的内容依赖于 regexp 是否具有全局标志 g。 如果没找到匹配结果返回 null

technical details

JavaScript version: 1.2


More examples

Examples

Global search string "ain", is not case sensitive:

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

n output:


try it"


String Object Reference JavaScript String Object