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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
---
title: String.prototype.match()
slug: Web/JavaScript/Reference/Global_Objects/String/match
translation_of: Web/JavaScript/Reference/Global_Objects/String/match
---
<p>{{JSRef}}</p>
<p> <strong><code>match()</code> </strong>方法检索返回一个字符串匹配正则表达式的结果。</p>
<div>{{EmbedInteractiveExample("pages/js/string-match.html", "shorter")}}</div>
<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox notranslate"><code><em>str</em>.match(regexp)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>regexp</code></dt>
<dd>一个<a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp" title="JavaScript/Reference/Global Objects/RegExp">正则表达式</a>对象。如果传入一个非正则表达式对象,则会隐式地使用 <code>new RegExp(obj)</code> 将其转换为一个 {{jsxref("RegExp")}} 。如果你没有给出任何参数并直接使用match() 方法 ,你将会得到一 个包含空字符串的 {{jsxref("Array")}} :[""] 。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<ul>
<li><span style="line-height: 1.572;">如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组。</span></li>
<li>如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组<span style="line-height: 1.572;">(<code>Array</code>)</span>。 在这种情况下,返回的项目将具有如下所述的其他属性。</li>
</ul>
<h4 id="附加属性">附加属性</h4>
<p>如上所述,匹配的结果包含如下所述的附加特性。</p>
<ul>
<li><code>groups</code>: 一个捕获组数组 或 {{jsxref("undefined")}}(如果没有定义命名捕获组)。</li>
<li><code>index</code>: 匹配的结果的开始位置</li>
<li><code>input</code>: 搜索的字符串.</li>
</ul>
<dl>
<dd>一个{{jsxref("Array")}},其内容取决于global(<code>g</code>)标志的存在与否,如果未找到匹配则为{{jsxref("null")}}。</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>如果正则表达式不包含 <code>g </code>标志,<code>str.match()</code> 将返回与 {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}}. 相同的结果。</p>
<h3 id="Notes" name="Notes">参看:<code>RegExp</code> 方法</h3>
<ul>
<li>如果你需要知道一个字符串是否与一个正则表达式匹配 {{jsxref("RegExp")}} ,可使用 {{jsxref("RegExp.prototype.test()", "RegExp.test()")}} 。</li>
<li>如果你只是需要第一个匹配结果,你也可以使用 {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}} 。</li>
<li>如果你想要获得捕获组,并且设置了全局标志,你需要用 {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}} 或者 {{jsxref("String.prototype.matchAll()", " String.prototype.matchAll()")}}</li>
</ul>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example_Using_match" name="Example:_Using_match">例子:使用 <code>match</code></h3>
<p>在下例中,使用 <code>match</code> 查找 "<code>Chapter</code>" 紧跟着 1 个或多个数值字符,再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 <code>i</code> 标志,因此大小写会被忽略。</p>
<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">'For more information, see Chapter 3.4.5.1'</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> re <span class="operator token">=</span> <span class="regex token">/see (chapter \d+(\.\d)*)/i</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> found <span class="operator token">=</span> str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span>re<span class="punctuation token">)</span><span class="punctuation token">;</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>found<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="comment token">// logs [ 'see Chapter 3.4.5.1',</span>
<span class="comment token">// 'Chapter 3.4.5.1',</span>
<span class="comment token">// '.1',</span>
<span class="comment token">// index: 22,</span>
<span class="comment token">// input: 'For more information, see Chapter 3.4.5.1' ]</span>
<span class="comment token">// 'see Chapter 3.4.5.1' 是整个匹配。</span>
<span class="comment token">// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。</span>
<span class="comment token">// '.1' 是被'(\.\d)'捕获的最后一个值。</span>
<span class="comment token">// 'index' 属性(22) 是整个匹配从零开始的索引。</span>
<span class="comment token">// 'input' 属性是被解析的原始字符串。</span></code></pre>
<h3 id="Example_Using_global_and_ignore_case_flags_with_match" name="Example:_Using_global_and_ignore_case_flags_with_match">例子:<code>match</code> 使用全局(global)和忽略大小写(ignore case)标志</h3>
<p>下例展示了 <code>match</code> 使用 global 和 ignore case 标志。A-E、a-e 的所有字母将会作为一个数组的元素返回。</p>
<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> regexp <span class="operator token">=</span> <span class="regex token">/[A-E]/gi</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> matches_array <span class="operator token">=</span> str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span>regexp<span class="punctuation token">)</span><span class="punctuation token">;</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>matches_array<span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="comment token">// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']</span></code></pre>
<h3 id="使用match,不传参数"><code>使用match(),不传参数 </code></h3>
<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">"Nothing will come of nothing."</span><span class="punctuation token">;</span>
str<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// returns [""]</span></code></pre>
<h3 id="一个非正则表达式对象作为参数">一个非正则表达式对象作为参数</h3>
<p>当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个 {{jsxref("RegExp")}}。如果它是一个有正号的正数,RegExp() 方法将忽略正号。</p>
<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> str1 <span class="operator token">=</span> <span class="string token">"NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript."</span><span class="punctuation token">,</span>
str2 <span class="operator token">=</span> <span class="string token">"My grandfather is 65 years old and My grandmother is 63 years old."</span><span class="punctuation token">,</span>
str3 <span class="operator token">=</span> <span class="string token">"The contract was declared null and void."</span><span class="punctuation token">;</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="string token">"number"</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// "number" 是字符串。返回["number"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">NaN</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// NaN的类型是number。返回["NaN"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// Infinity的类型是number。返回["Infinity"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">+</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回["Infinity"]</span>
str1<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">Infinity</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回["-Infinity"]</span>
str2<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="number token">65</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回["65"]</span>
str2<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="operator token">+</span><span class="number token">65</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 有正号的number。返回["65"]</span>
str3<span class="punctuation token">.</span><span class="function token">match</span><span class="punctuation token">(</span><span class="keyword token">null</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 返回["null"]</span></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>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initial definition. Implemented in JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.5.4.10', 'String.prototype.match')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-string.prototype.match', 'String.prototype.match')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.match', 'String.prototype.match')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</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>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</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>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Firefox_特殊注意">Firefox 特殊注意</h2>
<ul>
<li>从Gecko 27 {{geckoRelease(27)}}开始,此方法遵守ECMAScript 规范。当使用全局正则表达式调用 <code>match()时,</code>{{jsxref("RegExp.lastIndex")}} 属性(如果指定)会重置为 <code>0</code> ({{bug(501739)}})。</li>
<li>从 Gecko 39 {{geckoRelease(39)}}开始,不赞成使用非标准的标志参数,并抛出一个控制台警告({{bug(1142351)}})。</li>
<li>从 Gecko 47 {{geckoRelease(47)}} 开始 , non-release builds不再支持非标准的标志参数,并且将完全移除({{bug(1245801)}})。</li>
<li>从 Gecko 49 {{geckoRelease(49)}} 开始 , 不再支持非标准的标志参数 ({{bug(1108382)}}).</li>
</ul>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li>{{jsxref("RegExp")}}</li>
<li>{{jsxref("RegExp.prototype.exec()")}}</li>
<li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>
|