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/string/search/index.html | |
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/string/search/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/string/search/index.html | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html new file mode 100644 index 0000000000..66ab981508 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html @@ -0,0 +1,87 @@ +--- +title: String.prototype.search() +slug: Web/JavaScript/Reference/Global_Objects/String/search +tags: + - JavaScript + - 原型 + - 参考 + - 字符串 + - 方法 + - 正则表达式 +translation_of: Web/JavaScript/Reference/Global_Objects/String/search +--- +<div>{{JSRef}}</div> + +<p><strong><code>search()</code></strong> 方法执行正则表达式和 {{jsxref("String")}} 对象之间的一个搜索匹配。</p> + +<div>{{EmbedInteractiveExample("pages/js/string-search.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><var>str</var>.search(<var>regexp</var>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>regexp</code></dt> + <dd>一个{{jsxref("RegExp", "正则表达式(regular expression)")}}对象</dd> + <dd>如果传入一个非正则表达式对象 <code>regexp</code>,则会使用 <code>new RegExp(regexp)</code> 隐式地将其转换为正则表达式对象。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>如果匹配成功,则 <code>search()</code> 返回正则表达式在字符串中首次匹配项的索引;否则,返回 <code>-1</code>。</p> + +<h2 id="描述">描述</h2> + +<p>当你想要知道字符串中是否存在某个模式(pattern)时可使用 <code>search()</code>,类似于正则表达式的 {{jsxref("RegExp.test", "test()")}} 方法。当要了解更多匹配信息时,可使用 {{jsxref("String.match", "match()")}}(但会更慢一些),该方法类似于正则表达式的 {{jsxref("RegExp.exec", "exec()")}} 方法。</p> + +<h2 id="示例">示例</h2> + +<h3 id="例子:使用_search">例子:使用 <code>search()</code></h3> + +<p>下面的例子中用两个不同的正则表达式对同一个字符串执行搜索匹配,得到一个成功匹配(正数返回值)和一个失败匹配(-1)。</p> + +<pre class="brush: js notranslate">var str = "hey JudE"; +var re = /[A-Z]/g; +var re2 = /[.]/g; +console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J" +console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-string.prototype.search', 'String.prototype.search')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.String.search")}}</p> + +<h2 id="Gecko_注意事项">Gecko 注意事项</h2> + +<ul> + <li>在 {{Gecko("8.0")}} 及之前的版本,<code>search()</code> 的实现有问题:在调用该方法时,若不传递参数或者参数为 {{jsxref("undefined")}},该方法将不会匹配空字符串,而是匹配字符串 "undefined"。这个问题已被修复,现在,<code>"a".search()</code> 和 <code>"a".search(undefined)</code> 都能正确返回 0。</li> + <li>Starting with Gecko 39 {{geckoRelease(39)}}, the non-standard <code>flags</code> argument is deprecated and throws a console warning ({{bug(1142351)}}).</li> + <li>Starting with Gecko 47 {{geckoRelease(47)}}, the non-standard <code>flags</code> argument is no longer supported in non-release builds and will soon be removed entirely ({{bug(1245801)}}).</li> + <li>Starting with Gecko 49 {{geckoRelease(49)}}, the non-standard <code>flags</code> argument is no longer supported ({{bug(1108382)}}).</li> +</ul> + +<h2 id="参见">参见</h2> + +<ul> +<li><a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions">在 JavaScript 中使用正则表达式</a></li> + <li>{{jsxref("String.prototype.match()")}}</li> + <li>{{jsxref("RegExp.prototype.exec()")}}</li> +</ul> |