aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html
blob: 48e537cb96422260198a8e4a27a158ec8b86e464 (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
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
---
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>

<h2 id="Syntax">语法</h2>

<pre class="syntaxbox"><code><em>str</em>.match(regexp)</code></pre>

<h3 id="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>如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组。</li>
 <li>如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组(<code>Array</code>)。 在这种情况下,返回的项目将具有如下所述的其他属性。</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>

<p>一个{{jsxref("Array")}},其内容取决于global(<code>g</code>)标志的存在与否,如果未找到匹配则为{{jsxref("null")}}</p>

<h2 id="Description">描述</h2>

<p>如果正则表达式不包含 <code></code>标志,<code>str.match()</code> 将返回与 {{jsxref("RegExp.prototype.exec()", "RegExp.exec()")}}. 相同的结果。</p>

<h3 id="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">示例</h2>

<h3 id="Example_Using_match">例子:使用 <code>match</code></h3>

<p>在下例中,使用 <code>match</code> 查找 "<code>Chapter</code>" 紧跟着 1 个或多个数值字符,再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 <code>i</code> 标志,因此大小写会被忽略。</p>

<pre class="brush: js">var str = 'For more information, see Chapter 3.4.5.1';
var re = /see (chapter \d+(\.\d)*)/i;
var found = str.match(re);

console.log(found);

// logs [ 'see Chapter 3.4.5.1',
//        'Chapter 3.4.5.1',
//        '.1',
//        index: 22,
//        input: 'For more information, see Chapter 3.4.5.1' ]

// 'see Chapter 3.4.5.1' 是整个匹配。
// 'Chapter 3.4.5.1' 被'(chapter \d+(\.\d)*)'捕获。
// '.1' 是被'(\.\d)'捕获的最后一个值。
// 'index' 属性(22) 是整个匹配从零开始的索引。
// 'input' 属性是被解析的原始字符串。</pre>

<h3 id="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">var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/gi;
var matches_array = str.match(regexp);

console.log(matches_array);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']</pre>

<h3 id="使用match,不传参数"><code>使用match(),不传参数 </code></h3>

<pre class="brush: js">var str = "Nothing will come of nothing.";

str.match();   // returns [""]</pre>

<h3 id="一个非正则表达式对象作为参数">一个非正则表达式对象作为参数</h3>

<p>当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个 {{jsxref("RegExp")}}。如果它是一个有正号的正数,RegExp() 方法将忽略正号。</p>

<pre class="brush: js">var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
    str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
    str3 = "The contract was declared null and void.";
str1.match("number");   // "number" 是字符串。返回["number"]
str1.match(NaN);        // NaN的类型是number。返回["NaN"]
str1.match(Infinity);   // Infinity的类型是number。返回["Infinity"]
str1.match(+Infinity);  // 返回["Infinity"]
str1.match(-Infinity);  // 返回["-Infinity"]
str2.match(65);         // 返回["65"]
str2.match(+65);        // 有正号的number。返回["65"]
str3.match(null);       // 返回["null"]</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>

{{Compat}}

<h2 id="See_also">相关链接</h2>

<ul>
 <li>{{jsxref("RegExp")}}</li>
 <li>{{jsxref("RegExp.prototype.exec()")}}</li>
 <li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>