myString.match( expr ) in ECMAScript 262
Run the supplied regular expression against the string and return an array of the results.
Arguments
name | type | description |
---|---|---|
expr | RegExp | The regular expression to matc |
- Return Type
- Array
Description
If expr
has the global flag set (true), this returns an array of strings of all matches found by calling expr.exec(myString)
repeatedly.
If expr
has the global flag unset (false), this returns the same array as RegExp.exec(myString)
.
var description = "The various items cost $4.34, $7.12, $8, $8.25, and even $12.04";
var firstPriceRE = /\$\d*(\.\d{2})?/;
var allPricesRE = /\$\d*(\.\d{2})?/g;
var price = description.match(firstPriceRE);
// ** price is ['$4.34','.34']
var prices = description.match(allPricesRE);
// ** prices is ['$4.34','$7.12','$8','$8.25','$12.04']
To use a regular expression to test against a string, use RegExp.test()
instead of this method for better performance.