Symbol.match
指定了匹配的是正则表达式而不是字符串。String.prototype.match()
方法会调用此函数。
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.
描述
此函数还用于标识对象是否具有正则表达式的行为。比如, 假值),就表示该对象不打算用作正则表达式对象。
Symbol.match 属性的属性特性: |
|
---|---|
writable | false |
enumerable | false |
configurable | false |
示例
禁止表达式检查
下面代码会抛出一个 TypeError
:
"/bar/".startsWith(/bar/); // Throws TypeError, 因为 /bar/ 是一个正则表达式 // 且 Symbol.match 没有修改。
但是,如果你将 Symbol.match
置为 false
,使用 match
属性的表达式检查会认为该象不是正则表达式对象。startsWith
和 endsWith
方法将不会抛出 TypeError
。
var re = /foo/; re[Symbol.match] = false; "/foo/".startsWith(re); // true "/baz/".endsWith(re); // false
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Symbol.match |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) Symbol.match |
Draft |
浏览器支持
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
match |
Chrome Full support 50 | Edge No support No | Firefox Full support 40 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 50 | Chrome Android Full support 50 | Firefox Android Full support 40 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support