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

134 lines
6.9 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.

<article id="wikiArticle">
<div></div>
<p><strong><code>[@@search]()</code></strong> 方法执行了一个在给定字符串中的一个搜索以取得匹配正则模式的项。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><var>regexp</var>[Symbol.search](str)</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>str</code></dt>
<dd>搜索的目标 <a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a></dd>
</dl>
<h3 id="返回值">返回值</h3>
<dl>
<dt>整数</dt>
<dd>如果成功的话,<code>[@@search]()</code> 返回该正则模式的第一个匹配项的在字符串中的位置索引。否则将返回-1。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>这个方法在 <a href="Reference/Global_Objects/String/search" title="search() 方法执行正则表达式和 String对象之间的一个搜索匹配。"><code>String.prototype.search()</code></a> 的内部调用。例如,下面的两个方法返回相同结果。</p>
<pre><code class="language-javascript">'abc'.search(/a/);
/a/[Symbol.search]('abc');</code></pre>
<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p>
<h2 id="示例">示例</h2>
<h3 id="直接调用">直接调用</h3>
<p>这个方法的使用方式和 <a href="Reference/Global_Objects/String/search" title="search() 方法执行正则表达式和 String对象之间的一个搜索匹配。"><code>String.prototype.search()</code></a> 相同,不同之处是 <code>this</code> 和参数顺序。</p>
<pre><code class="language-javascript">var re = /-/g;
var str = '2016-01-02';
var result = re[Symbol.search](str);
console.log(result); // 4
</code></pre>
<h3 id="在子类中使用search">在子类中使用<code>@@search</code></h3>
<p>{jsxref("RegExp")}} 的子类可以覆写 <code>[@@search]()</code>方法来修改默认行为。</p>
<pre><code class="language-javascript">class MyRegExp extends RegExp {
constructor(str) {
super(str)
this.pattern = str;
}
[Symbol.search](str) {
return str.indexOf(this.pattern);
}
}
var re = new MyRegExp('a+b');
var str = 'ab a+b';
var result = str.search(re); // String.prototype.search calls re[@@search].
console.log(result); // 3
</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-regexp.prototype-@@search" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">RegExp.prototype[@@search]</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>初始定义</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-regexp.prototype-@@search" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">RegExp.prototype[@@search]</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><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></div>
<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><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><a href="/en-US/Firefox/Releases/49" title="Released on 2016-09-13.">49</a> (49)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</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: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>49.0 (49)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="另见">另见</h2>
<ul>
<li><a href="Reference/Global_Objects/String/search" title="search() 方法执行正则表达式和 String对象之间的一个搜索匹配。"><code>String.prototype.search()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/@@match" title="对正则表达式匹配字符串时,[@@match]()方法用于获取匹配结果。"><code>RegExp.prototype[@@match]()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/@@replace" title="[@@replace]() 方法会在一个字符串中用给定的替换器,替换所有符合正则模式的匹配项,并返回替换后的新字符串结果。用来替换的参数可以是一个字符串或是一个针对每次匹配的回调函数。"><code>RegExp.prototype[@@replace]()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/@@split" title="[@@split]() 方法切割 String 对象为一个其子字符串的数组 。"><code>RegExp.prototype[@@split]()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/exec" title="exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。"><code>RegExp.prototype.exec()</code></a></li>
<li><a href="Reference/Global_Objects/RegExp/test" title="test() 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回 true 或 false。"><code>RegExp.prototype.test()</code></a></li>
</ul>
</article>