diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/regexp/@@search | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/regexp/@@search')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html new file mode 100644 index 0000000000..c88398e1a2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/regexp/@@search/index.html @@ -0,0 +1,153 @@ +--- +title: 'RegExp.prototype[@@search]()' +slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@search +--- +<div>{{JSRef}}</div> + +<p><strong><code>[@@search]()</code></strong> 方法执行了一个在给定字符串中的一个搜索以取得匹配正则模式的项。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><var>regexp</var>[Symbol.search](str)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>str</code></dt> + <dd>搜索的目标 {{jsxref("String")}}。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>整数</dt> + <dd>如果成功的话,<code>[@@search]()</code> 返回该正则模式的第一个匹配项的在字符串中的位置索引。否则将返回-1。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>这个方法在 {{jsxref("String.prototype.search()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p> + +<pre class="brush: js">'abc'.search(/a/); + +/a/[Symbol.search]('abc');</pre> + +<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p> + +<h2 id="示例">示例</h2> + +<h3 id="直接调用">直接调用</h3> + +<p>这个方法的使用方式和 {{jsxref("String.prototype.search()")}} 相同,不同之处是 <code>this</code> 和参数顺序。</p> + +<pre class="brush: js">var re = /-/g; +var str = '2016-01-02'; +var result = re[Symbol.search](str); +console.log(result); // 4 +</pre> + +<h3 id="在子类中使用search">在子类中使用<code>@@search</code></h3> + +<p>{jsxref("RegExp")}} 的子类可以覆写 <code>[@@search]()</code>方法来修改默认行为。</p> + +<pre class="brush: js">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 +</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>{{SpecName('ES6', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@search', 'RegExp.prototype[@@search]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</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>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</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>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(49)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("String.prototype.search()")}}</li> + <li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li> + <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> + <li>{{jsxref("RegExp.prototype.test()")}}</li> +</ul> |