aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html
blob: 66ab981508a4b8260c809cd946f1c7ba45121c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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>