aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/guide/regular_expressions/quantifiers/index.html
blob: 1173553f2d9f913968132e327c6df2ca28f242bc (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
title: 量词
slug: Web/JavaScript/Guide/Regular_Expressions/Quantifiers
translation_of: Web/JavaScript/Guide/Regular_Expressions/Quantifiers
original_slug: Web/JavaScript/Guide/Regular_Expressions/量词
---
<p>{{jsSidebar("JavaScript Guide")}}</p>

<p>量词表示要匹配的字符或表达式的数量。</p>

<div>{{EmbedInteractiveExample("pages/js/regexp-quantifiers.html", "taller")}}</div>

<h2 id="类型">类型</h2>

<div class="hidden">The following table is also duplicated on <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet">this cheatsheet</a>. Do not forget to edit it as well, thanks!</div>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Characters</th>
   <th scope="col">Meaning</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code><em>x</em>*</code></td>
   <td>
    <p>将前面的项“x”匹配0次或更多次。例如,/bo*/匹配“A ghost booooed”中的“boooo”和“A bird warbled”中的“b”,但在“A goat grunt”中没有匹配。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>+</code></td>
   <td>
    <p>将前一项“x”匹配1次或更多次。等价于{1,}。例如,/a+/匹配“candy”中的“a”和“caaaaaaandy”中的“a”。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>?</code></td>
   <td>
    <p>将前面的项“x”匹配0或1次。例如,/e?le?/匹配angel中的el和angle中的le。</p>

    <p>如果立即在任何量词*、+、?或{}之后使用,则使量词是非贪婪的(匹配最小次数),而不是默认的贪婪的(匹配最大次数)。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>{<em>n</em>}</code></td>
   <td>
    <p>其中“n”是一个正整数,与前一项“x”的n次匹配。例如,<code>/a{2}/ </code>不匹配“candy”中的“a”,但它匹配“caandy”中的所有“a”,以及“caaandy”中的前两个“a”。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>{<em>n</em>,}</code></td>
   <td>
    <p>其中,“n”是一个正整数,与前一项“x”至少匹配“n”次。例如,<code>/a{2,}/</code>不匹配“candy”中的“a”,但匹配“caandy”和“caaaaaaandy”中的所有a。</p>
   </td>
  </tr>
  <tr>
   <td><code><em>x</em>{<em>n</em>,<em>m</em>}</code></td>
   <td>
    <p>其中,“n”是0或一个正整数,“m”是一个正整数,而m &gt; n至少与前一项“x”匹配,最多与“m”匹配。例如,/a{1,3}/不匹配“cndy”中的“a”,“candy”中的“a”,“caandy”中的两个“a”,以及“caaaaaaandy”中的前三个“a”。注意,当匹配“caaaaaaandy”时,匹配的是“aaa”,即使原始字符串中有更多的“a”。</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code><em>x</em>*?</code><br>
     <code><em>x</em>+?</code><br>
     <code><em>x</em>??</code><br>
     <code><em>x</em>{n}?</code><br>
     <code><em>x</em>{n,}?</code><br>
     <code><em>x</em>{n,m}?</code></p>
   </td>
   <td>
    <p>默认情况下,像 <code></code>和 <code></code>这样的量词是“贪婪的”,这意味着它们试图匹配尽可能多的字符串。?量词后面的字符使量词“非贪婪”:意思是它一旦找到匹配就会停止。例如,给定一个字符串“some &lt;foo&gt; &lt;bar&gt; new &lt;/bar&gt; &lt;/foo&gt; thing”:</p>

    <ul>
     <li><code>/&lt;.*&gt;/</code> will match "&lt;foo&gt; &lt;bar&gt; new &lt;/bar&gt; &lt;/foo&gt;"</li>
     <li><code>/&lt;.*?&gt;/</code> will match "&lt;foo&gt;"</li>
    </ul>
   </td>
  </tr>
 </tbody>
</table>

<h2 id="举例说明">举例说明</h2>

<h3 id="重复模式">重复模式</h3>

<pre class="brush: js">var wordEndingWithAs = /\w+a+/;
var delicateMessage = "This is Spartaaaaaaa";

console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]</pre>

<h3 id="计算字符集">计算字符集</h3>

<pre class="brush: js">var singleLetterWord = /\b\w\b/g;
var notSoLongWord = /\b\w{1,6}\b/g;
var loooongWord = /\b\w{13,}\b/g;

var sentence = "Why do I have to learn multiplication table?";

console.table(sentence.match(singleLetterWord)); // ["I"]
console.table(sentence.match(notSoLongWord));    // [ "Why", "do", "I", "have", "to", "learn", "table" ]
console.table(sentence.match(loooongWord));      // ["multiplication"]可选可选字符
</pre>

<h3 id="可选字符"> 可选字符</h3>

<pre class="brush: js">var britishText = "He asked his neighbour a favour.";
var americanText = "He asked his neighbor a favor.";

var regexpEnding = /\w+ou?r/g;
// \w+ One or several letters
// o   followed by an "o",
// u?  optionally followed by a "u"
// r   followed by an "r"

console.table(britishText.match(regexpEnding));
// ["neighbour", "favour"]

console.table(americanText.match(regexpEnding));
// ["neighbor", "favor"]
</pre>

<h3 id="贪婪_与_非贪婪的">贪婪 与 非贪婪的</h3>

<pre class="brush: js">var text = "I must be getting somewhere near the centre of the earth.";
var greedyRegexp = /[\w ]+/;
// [\w ]      a letter of the latin alphabet or a whitespace
//      +     one or several times

console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the centre of the earth"
// almost all of the text matches (leaves out the dot character)

var nonGreedyRegexp = /[\w ]+?/; // Notice the question mark
console.log(text.match(nonGreedyRegexp));
// "I"
// The match is the smallest one possible
</pre>

<h2 id="规范"><strong>规范</strong></h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-quantifier', 'RegExp: Quantifiers')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持">浏览器支持</h2>

<p>For browser compatibility information, check out the <a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Browser_compatibility">main Regular Expressions compatibility table</a>.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">Regular expressions guide</a>

  <ul>
   <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes">Character classes</a></li>
   <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions">Assertions</a></li>
   <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode property escapes</a></li>
   <li><a href="/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges">Groups and ranges</a></li>
  </ul>
 </li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp">The <code>RegExp()</code> constructor</a></li>
</ul>