[@@match]()
method retrieves the matches when matching a
string
against a
regular expression
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
regexp[Symbol.match](str)
str
String
that is a target of the match.
Array
containing the entire match result and any parentheses-captured matched results, or
null
if there were no matches.
This method is called internally in
String.prototype.match()
.
For example, the following two examples return same result.
'abc'.match(/a/);
/a/[Symbol.match]('abc');
This method exists for customizing match behavior within
RegExp
subclasses.
This method can be used in
almost
the same way as
String.prototype.match()
, except the different
this
and the different arguments order.
let re = /[0-9]+/g; let str = '2016-01-02'; let result = re[Symbol.match](str); console.log(result); // ["2016", "01", "02"]
@@match
in subclasses
Subclasses of
RegExp
can override the
[@@match]()
method to modify the default behavior.
class MyRegExp extends RegExp {
[Symbol.match](str) {
let result = RegExp.prototype[Symbol.match].call(this, str);
if (!result) return null;
return {
group(n) {
return result[n];
}
};
}
}
let re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)');
let str = '2016-01-02';
let result = str.match(re); // String.prototype.match calls re[@@match].
console.log(result.group(1)); // 2016
console.log(result.group(2)); // 01
console.log(result.group(3)); // 02
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.prototype[@@match]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@match
|
Chrome 50 | Edge 13 | Firefox 49 | IE No | Opera 37 | Safari 10 | WebView Android 50 | Chrome Android 50 | Firefox Android 49 | Opera Android 37 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
String.prototype.match()
RegExp.prototype[@@replace]()
RegExp.prototype[@@search]()
RegExp.prototype[@@split]()
RegExp.prototype.exec()
RegExp.prototype.test()
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()