String.match() (Method)

Searches a string using a regular expression and returns the matches in an array.

Availability:

ECMAScript edition - 3
JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Property/method value type:Array object
JavaScript syntax:-myObject.match(aPattern)
Argument list:aPatternA regular expression pattern

This is one of the additions to the String object to support regular expressions.

The matches are returned in an array with each match in a separate element. You can use this to strip numeric values out of a string containing words and numbers by looking for a pattern such as:

/\d+/g

This would yield all the numeric values from a string.

The g attribute affects whether a single item matches or whether all of them match.

When the g attribute is not used, the array object has an additional property named index, which contains the character location where the match occurred. It also has an additional property called input, which contains the original string that was searched for a match.

Warnings:

Example code:

   myString = "JavaScript is good";
   myLocation = myString.search(/GOOD/i);
   document.write(myLocation);

See also:Array.index, Array.input, RegExp pattern, RegExp.exec(), RegExp.lastIndex, Regular expression, String.replace(), String.search(), String.split()

Cross-references:

ECMA 262 edition 3 - section 15.5.4.10