String.prototype.match()

Summary

The match() method retrieves the matches when matching a string against a regular expression.

Syntax

str.match(regexp)

Parameters

regexp
A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

Returns

array
An Array containing the matched results or null if there were no matches.

Description

If the regular expression does not include the g flag, returns the same result as RegExp.exec(). The returned Array has an extra input property, which contains the original string that was parsed. In addition, it has an index property, which represents the zero-based index of the match in the string.

If the regular expression includes the g flag, the method returns an Array containing all matches. If there were no matches, the method returns null.

See also: RegExp methods

  • If you need to know if a string matches a regular expression RegExp, use search().
  • If you only want the first match found, you might want to use RegExp.exec() instead.

Examples

Example: Using match()

In the following example, match() is used to find 'Chapter' followed by 1 or more numeric characters followed by a decimal point and numeric character 0 or more times. The regular expression includes the i flag so that case will be ignored.

var str = 'For more information, see Chapter 3.4.5.1';
var re = /(chapter \d+(\.\d)*)/i;
var found = str.match(re);

console.log(found);

// logs ['Chapter 3.4.5.1', 'Chapter 3.4.5.1', '.1']

// 'Chapter 3.4.5.1' is the first match and the first value 
// remembered from `(Chapter \d+(\.\d)*)`.

// '.1' is the last value remembered from `(\.\d)`.

Example: Using global and ignore case flags with match()

The following example demonstrates the use of the global and ignore case flags with match(). All letters A through E and a through e are returned, each its own element in the array.

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

Specifications

SpecificationStatusComment
ECMAScript 3rd Edition.StandardInitial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.match' in that specification.
Standard 
ECMAScript 6 (ECMA-262)
The definition of 'String.prototype.match' in that specification.
Draft 

Browser compatibility

  • Desktop
  • Mobile
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)

Firefox-specific notes

  • Starting with Gecko 27 (Firefox 27 / Thunderbird 27 / SeaMonkey 2.24), this method has been adjusted to conform with the ECMAScript specification. When match() is called with a global regular expression, the RegExp.lastIndex property (if specified) will be reset to 0 (bug 501739).

See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值