2019-04-21 11:50:48 +08:00

125 lines
6.7 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><code><strong>Symbol.match </strong></code>指定了匹配的是正则表达式而不是字符串。<a href="Reference/Global_Objects/String/match" title="match() 方法检索返回一个字符串匹配正则表达式的的结果。"><code>String.prototype.match()</code></a> 方法会调用此函数。</p>
<div><table class="standard-table">
<thead>
<tr>
<th class="header" colspan="2"><code>Symbol.match</code> 属性的属性特性:</th>
</tr>
</thead>
<tbody>
<tr>
<td>writable</td>
<td>false</td>
</tr>
<tr>
<td>enumerable</td>
<td>false</td>
</tr>
<tr>
<td>configurable</td>
<td>false</td>
</tr>
</tbody>
</table></div>
<h2 id="描述">描述</h2>
<p>此函数还用于标识对象是否具有正则表达式的行为。比如, <a href="Reference/Global_Objects/String/startsWith" title="startsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 true 或 false。"><code>String.prototype.startsWith()</code></a><a href="Reference/Global_Objects/String/endsWith" title="endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。"><code>String.prototype.endsWith()</code></a><a href="Reference/Global_Objects/String/includes" title="includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。"><code>String.prototype.includes()</code></a> 这些方法会检查其第一个参数是否是正则表达式,是正则表达式就抛出一个<a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a>。现在,如果 <code>match</code> symbol 设置为 <code>false</code>(或者一个 <a class="glossaryLink" href="/en-US/docs/Glossary/Falsy" title="假值: A falsy value is a value that is considered false when encountered in a Boolean context.">假值</a>),就表示该对象不打算用作正则表达式对象。</p>
<h2 id="示例">示例</h2>
<h3 id="禁止表达式检查">禁止表达式检查</h3>
<p>下面代码会抛出一个 <a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<pre><code class="language-javascript">"/bar/".startsWith(/bar/);
// Throws TypeError, 因为 /bar/ 是一个正则表达式
// 且 Symbol.match 没有修改。</code></pre>
<p>但是,如果你将 <code>Symbol.match</code> 置为 <code>false使用 match 属性的</code>表达式检查会认为该象不是正则表达式对象。<code>startsWith</code><code>endsWith</code> 方法将不会抛出 <code>TypeError</code></p>
<pre><code class="language-javascript">var re = /foo/;
re[Symbol.match] = false;
"/foo/".startsWith(re); // true
"/baz/".endsWith(re); // false
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-symbol.match" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Symbol.match</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-symbol.match" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Symbol.match</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器支持">浏览器支持</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>50</td>
<td><a href="/en-US/Firefox/Releases/40" title="Released on 2015-08-11.">40</a> (40)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>40.0 (40)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Symbol/replace" title="Symbol.replace 这个属性指定了当一个字符串替换所匹配字符串时所调用的方法。String.prototype.replace() 方法会调用此方法。"><code>Symbol.replace</code></a></li>
<li><a href="Reference/Global_Objects/Symbol/search" title="Symbol.search 指定了一个搜索方法这个方法接受用户输入的正则表达式返回该正则表达式在字符串中匹配到的下标这个方法由以下的方法来调用 String.prototype.search()。"><code>Symbol.search</code></a></li>
<li><a href="Reference/Global_Objects/Symbol/split" title="Symbol.split 指向 一个正则表达式的索引处分割字符串的方法。 这个方法通过 String.prototype.split() 调用。"><code>Symbol.split</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/@@match" title="对正则表达式匹配字符串时,[@@match]()方法用于获取匹配结果。"><code>RegExp.prototype[@@match]()</code></a></li>
</ul>
</article>