RegExp.prototype[Symbol.match]()
[Symbol.match]() 方法是 RegExp 实例上用于指定 String.prototype.match() 应如何表现的方法。此外,它的存在(或缺失)会影响对象是否被视为正则表达式。
试一试
class RegExp1 extends RegExp {
[Symbol.match](str) {
const result = RegExp.prototype[Symbol.match].call(this, str);
if (result) {
return "VALID";
}
return "INVALID";
}
}
console.log("2012-07-02".match(new RegExp1("(\\d+)-(\\d+)-(\\d+)")));
// Expected output: "VALID"
语法
regexp[Symbol.match](str)
参数
返回值
一个 Array,其内容取决于全局标志(g)是否存在,如果未找到匹配项,则为 null。
- 如果使用了
g标志,将返回所有与整个正则表达式匹配的结果,但不包含捕获组。 - 如果未使用
g标志,则只返回第一个完整匹配及其相关的捕获组。在这种情况下,match()将返回与RegExp.prototype.exec()相同的结果(一个带有额外属性的数组)。
描述
此方法在 String.prototype.match() 中被内部调用。
例如,以下两个示例返回相同的结果。
"abc".match(/a/);
/a/[Symbol.match]("abc");
如果正则表达式是全局的(带有 g 标志),则将重复调用该正则表达式的 exec() 方法,直到 exec() 返回 null。否则,exec() 只会被调用一次,其结果将成为 [Symbol.match]() 的返回值。
因为 [Symbol.match]() 会持续调用 exec() 直到它返回 null,并且 exec() 在最后一个匹配失败时会自动将正则表达式的 lastIndex 重置为 0,所以 [Symbol.match]() 在退出时通常不会产生副作用。但是,当正则表达式是 粘性(sticky)但非全局时,lastIndex 不会被重置。在这种情况下,每次调用 match() 都可能返回不同的结果。
const re = /[abc]/y;
for (let i = 0; i < 5; i++) {
console.log("abc".match(re), re.lastIndex);
}
// [ 'a' ] 1
// [ 'b' ] 2
// [ 'c' ] 3
// null 0
// [ 'a' ] 1
当正则表达式是粘性且全局时,它仍然会执行粘性匹配 — 即,它将无法匹配 lastIndex 之后的任何出现。
console.log("ab-c".match(/[abc]/gy)); // [ 'a', 'b' ]
如果当前匹配为空字符串,lastIndex 仍会被推进 — 如果正则表达式是 Unicode 感知的,它将前进一个 Unicode 代码点;否则,它将前进一个 UTF-16 代码单元。
console.log("😄".match(/(?:)/g)); // [ '', '', '' ]
console.log("😄".match(/(?:)/gu)); // [ '', '' ]
此方法用于自定义 RegExp 子类中的匹配行为。
此外,[Symbol.match] 属性用于 检查一个对象是否为正则表达式。
示例
直接调用
此方法的使用方式与 String.prototype.match() 几乎相同,只是 this 的值和参数顺序不同。
const re = /\d+/g;
const str = "2016-01-02";
const result = re[Symbol.match](str);
console.log(result); // ["2016", "01", "02"]
在子类中使用 [Symbol.match]()
RegExp 的子类可以覆盖 [Symbol.match]() 方法来修改默认行为。
class MyRegExp extends RegExp {
[Symbol.match](str) {
const result = RegExp.prototype[Symbol.match].call(this, str);
if (!result) return null;
return {
group(n) {
return result[n];
},
};
}
}
const re = new MyRegExp("(\\d+)-(\\d+)-(\\d+)");
const str = "2016-01-02";
const result = str.match(re); // String.prototype.match calls re[Symbol.match]().
console.log(result.group(1)); // 2016
console.log(result.group(2)); // 01
console.log(result.group(3)); // 02
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-regexp.prototype-%symbol.match% |
浏览器兼容性
加载中…