-
Notifications
You must be signed in to change notification settings - Fork 22.5k
/
index.md
385 lines (358 loc) · 16.1 KB
/
index.md
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
---
title: Character classes
slug: Web/JavaScript/Guide/Regular_expressions/Character_classes
page-type: guide
---
{{jsSidebar("JavaScript Guide")}}
Character classes distinguish kinds of characters such as, for example, distinguishing between letters and digits.
{{EmbedInteractiveExample("pages/js/regexp-character-classes.html")}}
## Types
<table class="standard-table">
<thead>
<tr>
<th scope="col">Characters</th>
<th scope="col">Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>[xyz]<br />[a-c]</code>
</td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class"><strong>Character class:</strong></a>
Matches any one of the enclosed characters. You can
specify a range of characters by using a hyphen, but if the hyphen
appears as the first or last character enclosed in the square brackets,
it is taken as a literal hyphen to be included in the character class
as a normal character.
</p>
<p>
For example, <code>[abcd]</code> is the same as <code>[a-d]</code>.
They match the "b" in "brisket", and the "c" in "chop".
</p>
<p>
For example, <code>[abcd-]</code> and <code>[-abcd]</code> match the
"b" in "brisket", the "c" in "chop", and the "-" (hyphen) in
"non-profit".
</p>
<p>
For example, <code>[\w-]</code> is the same as
<code>[A-Za-z0-9_-]</code>. They both match the "b" in "brisket", the
"c" in "chop", and the "n" in "non-profit".
</p>
<p>
When the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets"><code>unicodeSets</code></a> (<code>v</code>) flag is enabled, the character class has some additional features. See the <a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class">character class</a> reference for more information.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code>[^xyz]<br />[^a-c]</code>
</p>
</td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class"><strong>Negated character class:</strong></a>
Matches anything that is not enclosed in the square brackets. You can specify a range
of characters by using a hyphen, but if the hyphen appears as the
first character after the <code>^</code> or the last character enclosed in the square brackets, it is taken as
a literal hyphen to be included in the character class as a normal
character. For example, <code>[^abc]</code> is the same as
<code>[^a-c]</code>. They initially match "o" in "bacon" and "h" in
"chop".
</p>
<div class="notecard note">
<p>
<strong>Note:</strong> The ^ character may also indicate the
<a
href="/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions"
>beginning of input</a
>.
</p>
</div>
</td>
</tr>
<tr>
<td><code>.</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Wildcard"><strong>Wildcard:</strong></a>
Matches any single character <em>except</em> line terminators:
<code>\n</code>, <code>\r</code>, <code>\u2028</code> or
<code>\u2029</code>. For example, <code>/.y/</code> matches "my" and
"ay", but not "yes", in "yes make my day", as there is no character before "y" in "yes". If the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll"><code>dotAll</code></a> (s) flag is enabled, also matches line terminators.
Inside a character class, the dot loses its special meaning and
matches a literal dot.
</p>
</td>
</tr>
<tr>
<td><code>\d</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>Digit character class escape:</strong></a>
Matches any digit (Arabic numeral). Equivalent to <code>[0-9]</code>.
For example, <code>/\d/</code> or <code>/[0-9]/</code> matches "2" in
"B2 is the suite number".
</p>
</td>
</tr>
<tr>
<td><code>\D</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>Non-digit character class escape:</strong></a>
Matches any character that is not a digit (Arabic numeral). Equivalent
to <code>[^0-9]</code>. For example, <code>/\D/</code> or
<code>/[^0-9]/</code> matches "B" in "B2 is the suite number".
</p>
</td>
</tr>
<tr>
<td><code>\w</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>Word character class escape:</strong></a>
Matches any alphanumeric character from the basic Latin alphabet,
including the underscore. Equivalent to <code>[A-Za-z0-9_]</code>. For
example, <code>/\w/</code> matches "a" in "apple", "5" in "$5.28", "3"
in "3D" and "m" in "Émanuel".
</p>
</td>
</tr>
<tr>
<td><code>\W</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>Non-word character class escape:</strong></a>
Matches any character that is not a word character from the basic
Latin alphabet. Equivalent to <code>[^A-Za-z0-9_]</code>. For example,
<code>/\W/</code> or <code>/[^A-Za-z0-9_]/</code> matches "%" in "50%"
and "É" in "Émanuel".
</p>
</td>
</tr>
<tr>
<td><code>\s</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>White space character class escape:</strong></a>
Matches a single white space character, including space, tab, form
feed, line feed, and other Unicode spaces. Equivalent to
<code>[\f\n\r\t\v\u0020\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]</code>. For example, <code>/\s\w*/</code> matches " bar" in "foo bar".
</p>
</td>
</tr>
<tr>
<td><code>\S</code></td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape"><strong>Non-white space character class escape:</strong></a>
Matches a single character other than white space. Equivalent to
<code>[^\f\n\r\t\v\u0020\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]</code>. For example, <code>/\S\w*/</code> matches "foo" in "foo bar".
</p>
</td>
</tr>
<tr>
<td><code>\t</code></td>
<td>Matches a horizontal tab.</td>
</tr>
<tr>
<td><code>\r</code></td>
<td>Matches a carriage return.</td>
</tr>
<tr>
<td><code>\n</code></td>
<td>Matches a linefeed.</td>
</tr>
<tr>
<td><code>\v</code></td>
<td>Matches a vertical tab.</td>
</tr>
<tr>
<td><code>\f</code></td>
<td>Matches a form-feed.</td>
</tr>
<tr>
<td><code>[\b]</code></td>
<td>
Matches a backspace. If you're looking for the word-boundary assertion
(<code>\b</code>), see
<a
href="/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions"
>Assertions</a
>.
</td>
</tr>
<tr>
<td><code>\0</code></td>
<td>Matches a NUL character. Do not follow this with another digit.</td>
</tr>
<tr>
<td>
<code>\c<em>X</em></code>
</td>
<td>
<p>
Matches a control character using
<a href="https://en.wikipedia.org/wiki/Caret_notation"
>caret notation</a
>, where "X" is a letter from A–Z (corresponding to code points
<code>U+0001</code><em>–</em><code>U+001A</code>). For example,
<code>/\cM\cJ/</code> matches "\r\n".
</p>
</td>
</tr>
<tr>
<td>
<code>\x<em>hh</em></code>
</td>
<td>
Matches the character with the code <code><em>hh</em></code> (two
hexadecimal digits).
</td>
</tr>
<tr>
<td>
<code>\u<em>hhhh</em></code>
</td>
<td>
Matches a UTF-16 code-unit with the value
<code><em>hhhh</em></code> (four hexadecimal digits).
</td>
</tr>
<tr>
<td>
<code>\u<em>{hhhh}</em> or <em>\u{hhhhh}</em></code>
</td>
<td>
(Only when the <code>u</code> flag is set.) Matches the character with
the Unicode value <code>U+<em>hhhh</em></code> or <code
>U+<em>hhhhh</em></code
>
(hexadecimal digits).
</td>
</tr>
<tr>
<td>
<code>\p{<em>UnicodeProperty</em>}</code>,
<code>\P{<em>UnicodeProperty</em>}</code>
</td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape"><strong>Unicode character class escape:</strong></a>
Matches a character based on its Unicode character properties: for example, emoji characters, or Japanese
<em>katakana</em> characters, or Chinese/Japanese Han/Kanji characters,
etc.).
</p>
</td>
</tr>
<tr>
<td><code>\</code></td>
<td>
<p>
Indicates that the following character should be treated specially, or
"escaped". It behaves one of two ways.
</p>
<ul>
<li>
For characters that are usually treated literally, indicates that
the next character is special and not to be interpreted literally.
For example, <code>/b/</code> matches the character "b". By placing
a backslash in front of "b", that is by using <code>/\b/</code>, the
character becomes special to mean match a word boundary.
</li>
<li>
For characters that are usually treated specially, indicates that
the next character is not special and should be interpreted
literally. For example, "*" is a special character that means 0 or
more occurrences of the preceding character should be matched; for
example, <code>/a*/</code> means match 0 or more "a"s. To match
<code>*</code> literally, precede it with a backslash; for example,
<code>/a\*/</code> matches "a*".
</li>
</ul>
<div class="notecard note">
<p>
<strong>Note:</strong> To match this character literally, escape it
with itself. In other words to search for <code>\</code> use
<code>/\\/</code>.
</p>
</div>
</td>
</tr>
<tr>
<td>
<code><em>x</em>|<em>y</em></code>
</td>
<td>
<p>
<a href="/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Disjunction"><strong>Disjunction:</strong></a>
Matches either "x" or "y". Each component, separated by a pipe (<code>|</code>), is called an <em>alternative</em>. For example,
<code>/green|red/</code> matches "green" in "green apple" and "red" in
"red apple".
</p>
<div class="notecard note">
<p>
<strong>Note:</strong> A disjunction is another way to specify "a set of choices", but it's not a character class. Disjunctions are not atoms — you need to use a <a href="/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Groups_and_backreferences">group</a> to make it part of a bigger pattern. <code>[abc]</code> is functionally equivalent to <code>(?:a|b|c)</code>.
</p>
</div>
</td>
</tr>
</tbody>
</table>
## Examples
### Looking for a series of digits
In this example, we match a sequence of 4 digits with `\d{4}`. `\b` indicates a [word boundary](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions) (i.e. do not start or end matching in the middle of a number sequence).
```js
const randomData = "015 354 8787 687351 3512 8735";
const regexpFourDigits = /\b\d{4}\b/g;
console.table(randomData.match(regexpFourDigits));
// ['8787', '3512', '8735']
```
See more examples in the [character class escape](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape) reference.
### Looking for a word (from the latin alphabet) starting with A
In this example, we match a word starting with the letter A. `\b` indicates a [word boundary](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions) (i.e. do not start matching in the middle of a word). `[aA]` indicates the letter "a" or "A". `\w+` indicates any character _from the Latin alphabet_, multiple times (`+` is a [quantifier](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Quantifiers)). Note that because we already match until there are no more word characters, an end `\b` boundary is not necessary.
```js
const aliceExcerpt =
"I'm sure I'm not Ada,' she said, 'for her hair goes in such long ringlets, and mine doesn't go in ringlets at all.";
const regexpWordStartingWithA = /\b[aA]\w+/g;
console.table(aliceExcerpt.match(regexpWordStartingWithA));
// ['Ada', 'and', 'at', 'all']
```
See more examples in the [character class escape](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape) reference.
### Looking for a word (from Unicode characters)
Instead of the Latin alphabet, we can use a range of Unicode characters to identify a word (thus being able to deal with text in other languages like Russian or Arabic). The "Basic Multilingual Plane" of Unicode contains most of the characters used around the world and we can use character classes and ranges to match words written with those characters.
```js
const nonEnglishText = "Приключения Алисы в Стране чудес";
const regexpBMPWord = /([\u0000-\u0019\u0021-\uFFFF])+/gu;
// BMP goes through U+0000 to U+FFFF but space is U+0020
console.table(nonEnglishText.match(regexpBMPWord));
["Приключения", "Алисы", "в", "Стране", "чудес"];
```
See more examples in the [Unicode character class escape](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape) reference.
### Counting vowels
In this example, we count the number of vowels (A, E, I, O, U, Y) in a text. The `g` flag is used to match all occurrences of the pattern in the text. The `i` flag is used to make the pattern case-insensitive, so it matches both uppercase and lowercase vowels.
```js
const aliceExcerpt =
"There was a long silence after this, and Alice could only hear whispers now and then.";
const regexpVowels = /[aeiouy]/gi;
console.log("Number of vowels:", aliceExcerpt.match(regexpVowels).length);
// Number of vowels: 26
```
## See also
- [Regular expressions](/en-US/docs/Web/JavaScript/Guide/Regular_expressions) guide
- [Assertions](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions) guide
- [Quantifiers](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Quantifiers) guide
- [Groups and backreferences](/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Groups_and_backreferences) guide
- [`RegExp`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
- [Regular expressions](/en-US/docs/Web/JavaScript/Guide/Regular_expressions) reference
- [Character class: `[...]`, `[^...]`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class)
- [Character class escape: `\d`, `\D`, `\w`, `\W`, `\s`, `\S`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class_escape)
- [Character escape: `\n`, `\u{...}`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_escape)
- [Disjunction: `|`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Disjunction)
- [Unicode character class escape: `\p{...}`, `\P{...}`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Unicode_character_class_escape)
- [Wildcard: `.`](/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Wildcard)