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
|
---
title: 数量詞
slug: Web/JavaScript/Guide/Regular_Expressions/Quantifiers
tags:
- Guide
- JavaScript
- Reference
- Regular Expressions
- quantifiers
- regex
translation_of: Web/JavaScript/Guide/Regular_Expressions/Quantifiers
---
<p>{{jsSidebar("JavaScript Guide")}}</p>
<p>数量詞は、一致させる文字や式の数を示します。</p>
<div>{{EmbedInteractiveExample("pages/js/regexp-quantifiers.html", "taller")}}</div>
<h2 id="Types">種類</h2>
<div class="notecard note">
<p><strong>注:</strong> 以下の表の中で、<em>項目</em>は単一の文字だけでなく、<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes">文字クラス</a>、<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode プロパティエスケープ</a>、<a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges">グループと範囲</a>を示すこともあります。</p>
</div>
<table class="standard-table">
<thead>
<tr>
<th scope="col">文字</th>
<th scope="col">意味</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><em>x</em>*</code></td>
<td>
<p>直前の項目 "x" の 0 回以上の繰り返しに一致します。例えば <code>/bo*/</code> は "A ghost booooed" の "boooo" や "A bird warbled" の "b" に一致しますが、 "A goat grunted" には一致しません。</p>
</td>
</tr>
<tr>
<td><code><em>x</em>+</code></td>
<td>
<p>直前の項目 "x" の 1 回以上の繰り返しに一致します。<code>{1,}</code> と同等です。例えば <code>/a+/</code> は "candy" の "a" や "caaaaaaandy" のすべての "a" に一致します。</p>
</td>
</tr>
<tr>
<td><code><em>x</em>?</code></td>
<td>
<p>直前の項目 "x" の 0 回か 1 回の出現に一致します。例えば <code>/e?le?/</code> は "angel" の "el" や "angle" の "le" に一致します。</p>
<p><code>*</code>、<code>+</code>、<code>?</code>、<code>{}</code> といった量指定子の直後に使用した場合、既定とは逆に、その量指定子を非貪欲 (出現回数が最小のものに一致) とします。既定は貪欲 (出現回数が最大のものに一致) です。</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" には一致しませんが、"caaandy" の最初の 2 個の "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" には "n" より大きい正の整数が入ります。直前の項目 "x" が少なくとも "n" 回、多くても "m" 回出現するものに一致します。例えば <code>/a{1,3}/</code> は "cndy" では一致せず、"candy" の 'a'、"caandy" の 最初の 2 個の "a"、"caaaaaaandy" の最初の 3 個の "a" に一致します。"caaaaaaandy" では元の文字列に "a" が 4 個以上ありますが、一致するのは "aaa" であることに注意してください。</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> といった数量詞は貪欲です。つまり、できる限り多くの文字列と一致しようとします。数量詞の後にある <code>?</code> の文字は数量詞を非貪欲にします。つまり、一致が見つかるとすぐに停止します。例えば、"some <foo> <bar> new </bar> </foo> thing" といった文字列が与えられた場合は、</p>
<ul>
<li><code>/<.*>/</code> は "<foo> <bar> new </bar> </foo>" に一致します。</li>
<li><code>/<.*?>/</code> は "<foo>" に一致します。</li>
</ul>
</td>
</tr>
</tbody>
</table>
<h2 id="Examples">例</h2>
<h3 id="Repeated_pattern">繰り返しパターン</h3>
<pre class="brush: js">var wordEndingWithAs = /\w+a+\b/;
var delicateMessage = "This is Spartaaaaaaa";
console.table(delicateMessage.match(wordEndingWithAs)); // [ "Spartaaaaaaa" ]
</pre>
<h3 id="Counting_characters">文字数</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="Optional_character">省略可能な文字</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+ 1 つ以上の文字
// o "o" が続く
// u? 省略可能で "u" が続く
// r "r" が続く
console.table(britishText.match(regexpEnding));
// ["neighbour", "favour"]
console.table(americanText.match(regexpEnding));
// ["neighbor", "favor"]
</pre>
<h3 id="Greedy_versus_non-greedy">貪欲と非貪欲</h3>
<pre class="brush: js">var text = "I must be getting somewhere near the centre of the earth.";
var greedyRegexp = /[\w ]+/;
// [\w ] ラテンアルファベットまたは空白
// + 1 回以上
console.log(text.match(greedyRegexp)[0]);
// "I must be getting somewhere near the centre of the earth."
// テキストのすべてに一致 (ピリオドを除く)
var nonGreedyRegexp = /[\w ]+?/; // クエスチョンマークに注目
console.log(text.match(nonGreedyRegexp));
// "I"
// 一致する箇所は取りうる最も短い 1 文字
</pre>
<h2 id="関連情報">関連情報</h2>
<ul>
<li><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions">正規表現ガイド</a>
<ul>
<li><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes">文字クラス</a></li>
<li><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions">言明</a></li>
<li><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes">Unicode プロパティエスケープ</a></li>
<li><a href="/ja/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges">グループと範囲</a></li>
</ul>
</li>
<li><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp()</code> コンストラクター</a></li>
<li><a href="https://tc39.es/ecma262/#sec-quantifier">Quantifiers in the ECMAScript specification</a></li>
</ul>
|