match (String - JavaScript)

Gets substrings that match a regular expression.

Defined in

String (Standard - JavaScript)

Syntax

match(regExp:RegExp) : string[]

Parameters Description
regExp A regular expression. This can be a literal or a RegExp object.
Return value Description
string[] The matching string or strings.

Examples

(1) This example finds a regular expression literal.
var cities = new String("Paris   Moscow   Tokyo");
var regexp = /(Moscow)/;
var a = cities.match(regexp);
if(a == null)
	return "No match";
else
	return a[0];

(2) This example finds a regular expression object.

var cities = new String("Paris   Moscow   Tokyo");
var regexp = new RegExp("(moscow)", "i");
var a = cities.match(regexp);
if(a == null)
	return "No match";
else
	return a[0];