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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
---
title: Espressioni regolari
slug: Web/JavaScript/Guida/Espressioni_Regolari
translation_of: Web/JavaScript/Guide/Regular_Expressions
---
<div>{{jsSidebar("Guida JavaScript")}} {{PreviousNext("Web/JavaScript/Guide/Text_formatting", "Web/JavaScript/Guide/Indexed_collections")}}</div>
<p class="summary">Le espressioni regolari sono schemi usati per confrontare combinazioni di caratteri nelle stringhe. In JavaScript, le espressioni regolari sono anche oggetti. Questi pattern sono usati con i metodi {{jsxref("RegExp.exec", "exec()")}} e {{jsxref("RegExp.test", "test()")}} della classe {{jsxref("RegExp")}}, e con i metodi {{jsxref("String.match", "match()")}}, {{jsxref("String.matchAll", "matchAll()")}}, {{jsxref("String.replace", "replace()")}}, {{jsxref("String.search", "search()")}}, e {{jsxref("String.split", "split()")}} della classe {{jsxref("String")}}. Questo capitolo descrive le espressioni regolari in JavaScript.</p>
<h2 id="Creazione_di_unespressione_regolare">Creazione di un'espressione regolare</h2>
<p>Puoi creare un'espressione regolare in uno dei seguenti modi:</p>
<ul>
<li>
<p>Usando un'espressione regolare letterale, che consiste in uno schema racchiuso tra slash, come segue:</p>
<pre class="brush: js">var re = /ab+c/;
</pre>
<p>Le Espressioni Regolari letterali forniscono la compilazione dell'espressione regolare quando lo script è caricato. Se l'espressione regolare rimane costante, usare questo modo può migliorare le prestazioni.</p>
</li>
<li>
<p>O chiamando il costruttore dell'oggetto {{jsxref("RegExp")}} object, come segue:</p>
<pre class="brush: js">var re = new RegExp("ab+c");
</pre>
<p>Usando il costruttore avviene una compilazione runtime dell'espressione regolare. Usa il costruttore quando sai che lo schema dell'espressione regolare cambierà, o non sai lo schema a priori ma lo devi ricavare da un'altra risorsa, come un input dell'utente.</p>
</li>
</ul>
<h2 id="Scrivere_uno_schema_per_espressioni_regolari">Scrivere uno schema per espressioni regolari</h2>
<p>Uno schema di espressione regolare è composto da caratteri semplici, come <code>/abc/</code>, o da una combinazione di caratteri semplici e speciali, come <code>/ab*c/</code> o <code>/Chapter (\d+)\.\d*/</code>. L'ultimo esempio include parentesi che sono usate come un dispositivo di memoria. Il confronto fatto con queste parti dello schema è ricordato per usi futuri, come descritto in {{ web.link("#Using_parenthesized_substring_matches", "Using parenthesized substring matches") }}.</p>
<div class="blockIndicator note">
<p><strong>Nota:</strong> Se hai già familiarità con la struttura di un'espressione regolare, potresti anche leggere <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet">il cheatsheet</a> per una rapida ricerca di un modello/costrutto specifico</p>
</div>
<h3 id="Usare_modelli_semplici">Usare modelli semplici</h3>
<p><span class="tlid-translation translation" lang="it"><span title="">I modelli semplici sono costituiti da carattrei per i quali si desidera trovare una corrispondenza diretta.</span> <span title="">Ad esempio, il modello</span></span> <code>/abc/</code> <span class="tlid-translation translation" lang="it"><span title=""> corrisponde solo quando esattamente i caratteri </span></span> <code>"abc"</code> <span class="tlid-translation translation" lang="it"><span title=""> si presentano insieme e in quell'ordine.</span> <span title="">Una tale corrispondenza avrebbe successo nelle stringhe </span></span> <code>"Ciao, conosci il tuo abc?"</code> e <span class="tlid-translation translation" lang="it"> <span title=""><code>"Gli ultimi progetti di aeroplani si sono evoluti da slabcraft"</code>.</span> <span title="">In entrambi i casi la corrispondenza con la sottostringa <code>"abc"</code> avviene.</span> <span title="">Non c'è corrispondenza nella stringa <code>"Grab crab"</code> perché invece di contenere l'esatta sottostringa <code>"abc"</code> coniente la sottostringa <code>"ab c".</code></span></span></p>
<h3 id="Usare_caratteri_speciali">Usare caratteri speciali</h3>
<p><span class="tlid-translation translation" lang="it"><span title="">Quando la ricerca di una corrispondenza richiede qualcosa di più di una corrispondenza diretta, come la ricerca di una o più <em>b</em> o la ricerca di spazi bianchi, il modello include caratteri speciali.</span></span> Ad esempio, per abbinare una singola <code>"a" </code>seguita da zero o più <code>"b"</code> seguita da <code>"c"</code>, dovresti usare il modello <code>/ab*c/</code>: il <code>*</code> dopo <code>"b"</code> significa "0 o più occorrenze dell'elemento precedente". Nella stringa <code>"cbbabbbbcdebc"</code>, questo modello corrisponderà alla sottostringa <code>"abbbbc"</code>.<br>
<br>
<span class="tlid-translation translation" lang="it"><span title="">La tabella seguente fornisce un elenco completo e una descrizione dei caratteri speciali che possono essere utilizzati nelle espressioni regolari.</span></span></p>
<table class="standard-table">
<caption>Caratteri speciali nelle espressioni regolari</caption>
<thead>
<tr>
<th scope="col">Carattere</th>
<th scope="col">Significato/Utilizzo</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#special-backslash" id="special-backslash" name="special-backslash"><code>\</code></a></td>
<td>
<p>Matches according to the following rules:<br>
<br>
A backslash that precedes a non-special character indicates that the next character is special and is not to be interpreted literally. For example, a '<code>b</code>' without a preceding '\' generally matches lowercase 'b's wherever they occur. But a '<code>\b</code>' by itself doesn't match any character; it forms the special <a href="#special-word-boundary" title="#special-word-boundary">word boundary character</a>.<br>
<br>
A backslash that precedes a special character indicates that the next character is not special and should be interpreted literally. For example, the pattern <code>/a*/</code> relies on the special character '<code>*</code>' to match 0 or more a's. By contrast, the pattern <code>/a\*/</code> removes the specialness of the '<code>*</code>' to enable matches with strings like 'a*'.<br>
<br>
Do not forget to escape \ itself while using the RegExp("pattern") notation because \ is also an escape character in strings.</p>
</td>
</tr>
<tr>
<td><a href="#special-caret" id="special-caret" name="special-caret"><code>^</code></a></td>
<td>Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character.<br>
<br>
For example, <code>/^A/</code> does not match the 'A' in "an A", but does match the 'A' in "An E".<br>
<br>
The '<code>^</code>' has a different meaning when it appears as the first character in a character set pattern. See <a href="#special-negated-character-set" title="#special-negated-character-set">complemented character sets</a> for details and an example.</td>
</tr>
<tr>
<td><a href="#special-dollar" id="special-dollar" name="special-dollar"><code>$</code></a></td>
<td>
<p>Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character.</p>
<p>For example, <code>/t$/</code> does not match the 't' in "eater", but does match it in "eat".</p>
</td>
</tr>
<tr>
<td><a href="#special-asterisk" id="special-asterisk" name="special-asterisk"><code>*</code></a></td>
<td>
<p>Matches the preceding expression 0 or more times. Equivalent to {0,}.</p>
<p>For example, <code>/bo*/</code> matches 'boooo' in "A ghost booooed" and 'b' in "A bird warbled", but nothing in "A goat grunted".</p>
</td>
</tr>
<tr>
<td><a href="#special-plus" id="special-plus" name="special-plus"><code>+</code></a></td>
<td>
<p>Matches the preceding expression 1 or more times. Equivalent to <code>{1,}</code>.</p>
<p>For example, <code>/a+/</code> matches the 'a' in "candy" and all the a's in "caaaaaaandy", but nothing in "cndy".</p>
</td>
</tr>
<tr>
<td><a href="#special-questionmark" id="special-questionmark" name="special-questionmark"><code>?</code></a></td>
<td>Matches the preceding expression 0 or 1 time. Equivalent to <code>{0,1}</code>.<br>
<br>
For example, <code>/e?le?/</code> matches the 'el' in "angel" and the 'le' in "angle" and also the 'l' in "oslo".<br>
<br>
If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the fewest possible characters), as opposed to the default, which is greedy (matching as many characters as possible). For example, applying <code>/\d+/</code> to "123abc" matches "123". But applying <code>/\d+?/</code> to that same string matches only the "1".<br>
<br>
Also used in lookahead assertions, as described in the <code>x(?=y)</code> and <code>x(?!y)</code> entries of this table.<br>
</td>
</tr>
<tr>
<td><a href="#special-dot" id="special-dot" name="special-dot"><code>.</code></a></td>
<td>
<p>(The decimal point) matches any single character except the newline character.</p>
<p>For example, <code>/.n/</code> matches 'an' and 'on' in "nay, an apple is on the tree", but not 'nay'.</p>
</td>
</tr>
<tr>
<td><a href="#special-capturing-parentheses" id="special-capturing-parentheses" name="special-capturing-parentheses"><code>(x)</code></a></td>
<td>
<p>Matches 'x' and remembers the match, as the following example shows. The parentheses are called <em>capturing parentheses</em>.<br>
<br>
The '<code>(foo)</code>' and '<code>(bar)</code>' in the pattern <code>/(foo) (bar) \1 \2/</code> match and remember the first two words in the string "foo bar foo bar". The <code>\1 </code>and <code>\2</code> in the pattern match the string's last two words. Note that <code>\1, \2, \n</code> are used in the matching part of the regex. In the replacement part of a regex the syntax <code>$1, $2, $n</code> must be used, e.g.: <code>'bar foo'.replace( /(...) (...)/, '$2 $1' )</code>.</p>
</td>
</tr>
<tr>
<td><a href="#special-non-capturing-parentheses" id="special-non-capturing-parentheses" name="special-non-capturing-parentheses"><code>(?:x)</code></a></td>
<td>Matches 'x' but does not remember the match. The parentheses are called <em>non-capturing parentheses</em>, and let you define subexpressions for regular expression operators to work with. Consider the sample expression <code>/(?:foo){1,2}/</code>. If the expression was <code>/foo{1,2}/</code>, the <code>{1,2}</code> characters would apply only to the last 'o' in 'foo'. With the non-capturing parentheses, the <code>{1,2}</code> applies to the entire word 'foo'.</td>
</tr>
<tr>
<td><a href="#special-lookahead" id="special-lookahead" name="special-lookahead"><code>x(?=y)</code></a></td>
<td>
<p>Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead.</p>
<p>For example, <code>/Jack(?=Sprat)/</code> matches 'Jack' only if it is followed by 'Sprat'. <code>/Jack(?=Sprat|Frost)/</code> matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.</p>
</td>
</tr>
<tr>
<td><a href="#special-negated-look-ahead" id="special-negated-look-ahead" name="special-negated-look-ahead"><code>x(?!y)</code></a></td>
<td>
<p>Matches 'x' only if 'x' is not followed by 'y'. This is called a negated lookahead.</p>
<p>For example, <code>/\d+(?!\.)/</code> matches a number only if it is not followed by a decimal point. The regular expression <code>/\d+(?!\.)/.exec("3.141")</code> matches '141' but not '3.141'.</p>
</td>
</tr>
<tr>
<td><a href="#special-or" id="special-or" name="special-or"><code>x|y</code></a></td>
<td>
<p>Matches either 'x' or 'y'.</p>
<p>For example, <code>/green|red/</code> matches 'green' in "green apple" and 'red' in "red apple."</p>
</td>
</tr>
<tr>
<td><a href="#special-quantifier" id="special-quantifier" name="special-quantifier"><code>{n}</code></a></td>
<td>Matches exactly n occurrences of the preceding expression. N must be a positive integer.<br>
<br>
For example, <code>/a{2}/</code> doesn't match the 'a' in "candy," but it does match all of the a's in "caandy," and the first two a's in "caaandy."</td>
</tr>
<tr>
<td><a href="#special-quantifier-range" id="special-quantifier-range" name="special-quantifier-range"><code>{n,m}</code></a></td>
<td>
<p>Where <code>n</code> and <code>m</code> are positive integers and <code>n <= m</code>. Matches at least <code>n</code> and at most <code>m</code> occurrences of the preceding expression. When <code>m</code> is omitted, it's treated as ∞.</p>
<p>For example, <code>/a{1,3}/</code> matches nothing in "cndy", the 'a' in "candy," the first two a's in "caandy," and the first three a's in "caaaaaaandy". Notice that when matching "caaaaaaandy", the match is "aaa", even though the original string had more a's in it.</p>
</td>
</tr>
<tr>
<td><a href="#special-character-set" id="special-character-set" name="special-character-set"><code>[xyz]</code></a></td>
<td>Character set. This pattern type matches any one of the characters in the brackets, including <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Unicode_escape_sequences" title="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Unicode_escape_sequences">escape sequences</a>. Special characters like the dot(<code>.</code>) and asterisk (<code>*</code>) are not special inside a character set, so they don't need to be escaped. You can specify a range of characters by using a hyphen, as the following examples illustrate.<br>
<br>
The pattern<code> [a-d]</code>, which performs the same match as <code>[abcd]</code>, matches the 'b' in "brisket" and the 'c' in "city". The patterns <code>/[a-z.]+/ </code>and <code>/[\w.]+/</code> match the entire string "test.i.ng".</td>
</tr>
<tr>
<td><a href="#special-negated-character-set" id="special-negated-character-set" name="special-negated-character-set"><code>[^xyz]</code></a></td>
<td>
<p>A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen. Everything that works in the normal character set also works here.</p>
<p>For example, <code>[^abc]</code> is the same as <code>[^a-c]</code>. They initially match 'r' in "brisket" and 'h' in "chop."</p>
</td>
</tr>
<tr>
<td><a href="#special-backspace" id="special-backspace" name="special-backspace"><code>[\b]</code></a></td>
<td>Matches a backspace (U+0008). You need to use square brackets if you want to match a literal backspace character. (Not to be confused with <code>\b</code>.)</td>
</tr>
<tr>
<td><a href="#special-word-boundary" id="special-word-boundary" name="special-word-boundary"><code>\b</code></a></td>
<td>
<p>Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero. (Not to be confused with <code>[\b]</code>.)</p>
<p>Examples:<br>
<code>/\bm/</code> matches the 'm' in "moon" ;<br>
<code>/oo\b/</code> does not match the 'oo' in "moon", because 'oo' is followed by 'n' which is a word character;<br>
<code>/oon\b/</code> matches the 'oon' in "moon", because 'oon' is the end of the string, thus not followed by a word character;<br>
<code>/\w\b\w/</code> will never match anything, because a word character can never be followed by both a non-word and a word character.</p>
<div class="note">
<p><strong>Note:</strong> JavaScript's regular expression engine defines a <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6">specific set of characters</a> to be "word" characters. Any character not in that set is considered a word break. This set of characters is fairly limited: it consists solely of the Roman alphabet in both upper- and lower-case, decimal digits, and the underscore character. Accented characters, such as "é" or "ü" are, unfortunately, treated as word breaks.</p>
</div>
</td>
</tr>
<tr>
<td><a href="#special-non-word-boundary" id="special-non-word-boundary" name="special-non-word-boundary"><code>\B</code></a></td>
<td>
<p>Matches a non-word boundary. This matches a position where the previous and next character are of the same type: Either both must be words, or both must be non-words. The beginning and end of a string are considered non-words.</p>
<p>For example, <code>/\B../</code> matches 'oo' in "noonday", and <code>/y\B./</code> matches 'ye' in "possibly yesterday."</p>
</td>
</tr>
<tr>
<td><a href="#special-control" id="special-control" name="special-control"><code>\c<em>X</em></code></a></td>
<td>
<p>Where <em>X</em> is a character ranging from A to Z. Matches a control character in a string.</p>
<p>For example, <code>/\cM/</code> matches control-M (U+000D) in a string.</p>
</td>
</tr>
<tr>
<td><a href="#special-digit" id="special-digit" name="special-digit"><code>\d</code></a></td>
<td>
<p>Matches a digit character. Equivalent to <code>[0-9]</code>.</p>
<p>For example, <code>/\d/</code> or <code>/[0-9]/</code> matches '2' in "B2 is the suite number."</p>
</td>
</tr>
<tr>
<td><a href="#special-non-digit" id="special-non-digit" name="special-non-digit"><code>\D</code></a></td>
<td>
<p>Matches any non-digit character. Equivalent to <code>[^0-9]</code>.</p>
<p>For example, <code>/\D/</code> or <code>/[^0-9]/</code> matches 'B' in "B2 is the suite number."</p>
</td>
</tr>
<tr>
<td><a href="#special-form-feed" id="special-form-feed" name="special-form-feed"><code>\f</code></a></td>
<td>Matches a form feed (U+000C).</td>
</tr>
<tr>
<td><a href="#special-line-feed" id="special-line-feed" name="special-line-feed"><code>\n</code></a></td>
<td>Matches a line feed (U+000A).</td>
</tr>
<tr>
<td><a href="#special-carriage-return" id="special-carriage-return" name="special-carriage-return"><code>\r</code></a></td>
<td>Matches a carriage return (U+000D).</td>
</tr>
<tr>
<td><a href="#special-white-space" id="special-white-space" name="special-white-space"><code>\s</code></a></td>
<td>
<p>Matches a single white space character, including space, tab, form feed, line feed. Equivalent to <code>[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]</code>.</p>
<p>For example, <code>/\s\w*/</code> matches ' bar' in "foo bar."</p>
</td>
</tr>
<tr>
<td><a href="#special-non-white-space" id="special-non-white-space" name="special-non-white-space"><code>\S</code></a></td>
<td>
<p>Matches a single character other than white space. Equivalent to <code>[^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]</code>.</p>
<p>For example, <code>/\S\w*/</code> matches 'foo' in "foo bar."</p>
</td>
</tr>
<tr>
<td><a href="#special-tab" id="special-tab" name="special-tab"><code>\t</code></a></td>
<td>Matches a tab (U+0009).</td>
</tr>
<tr>
<td><a href="#special-vertical-tab" id="special-vertical-tab" name="special-vertical-tab"><code>\v</code></a></td>
<td>Matches a vertical tab (U+000B).</td>
</tr>
<tr>
<td><a href="#special-word" id="special-word" name="special-word"><code>\w</code></a></td>
<td>
<p>Matches any alphanumeric character including the underscore. Equivalent to <code>[A-Za-z0-9_]</code>.</p>
<p>For example, <code>/\w/</code> matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."</p>
</td>
</tr>
<tr>
<td><a href="#special-non-word" id="special-non-word" name="special-non-word"><code>\W</code></a></td>
<td>
<p>Matches any non-word character. Equivalent to <code>[^A-Za-z0-9_]</code>.</p>
<p>For example, <code>/\W/</code> or <code>/[^A-Za-z0-9_]/</code> matches '%' in "50%."</p>
</td>
</tr>
<tr>
<td><a href="#special-backreference" id="special-backreference" name="special-backreference"><code>\<em>n</em></code></a></td>
<td>
<p>Where <em>n</em> is a positive integer, a back reference to the last substring matching the <em>n</em> parenthetical in the regular expression (counting left parentheses).</p>
<p>For example, <code>/apple(,)\sorange\1/</code> matches 'apple, orange,' in "apple, orange, cherry, peach."</p>
</td>
</tr>
<tr>
<td><a href="#special-null" id="special-null" name="special-null"><code>\0</code></a></td>
<td>Matches a NULL (U+0000) character. Do not follow this with another digit, because <code>\0<digits></code> is an octal <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Unicode_escape_sequences" title="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Unicode_escape_sequences">escape sequence</a>.</td>
</tr>
<tr>
<td><a href="#special-hex-escape" id="special-hex-escape" name="special-hex-escape"><code>\xhh</code></a></td>
<td>Matches the character with the code hh (two hexadecimal digits)</td>
</tr>
<tr>
<td><a href="#special-unicode-escape" id="special-unicode-escape" name="special-unicode-escape"><code>\uhhhh</code></a></td>
<td>Matches the character with the code hhhh (four hexadecimal digits).</td>
</tr>
<tr>
<td><a href="#special-unicode-escape-es6" id="special-unicode-escape-es6" name="special-unicode-escape-es6"><code>\u{hhhh}</code></a></td>
<td>(only when u flag is set) Matches the character with the Unicode value hhhh (hexadecimal digits).</td>
</tr>
</tbody>
</table>
<p>Escaping user input to be treated as a literal string within a regular expression can be accomplished by simple replacement:</p>
<pre class="brush: js">function escapeRegExp(string){
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}</pre>
<h3 id="Using_parentheses">Using parentheses</h3>
<p>Parentheses around any part of the regular expression pattern cause that part of the matched substring to be remembered. Once remembered, the substring can be recalled for other use, as described in {{ web.link("#Using_parenthesized_substring_matches", "Using Parenthesized Substring Matches") }}.</p>
<p>For example, the pattern <code>/Chapter (\d+)\.\d*/</code> illustrates additional escaped and special characters and indicates that part of the pattern should be remembered. It matches precisely the characters 'Chapter ' followed by one or more numeric characters (<code>\d</code> means any numeric character and <code>+</code> means 1 or more times), followed by a decimal point (which in itself is a special character; preceding the decimal point with \ means the pattern must look for the literal character '.'), followed by any numeric character 0 or more times (<code>\d</code> means numeric character, <code>*</code> means 0 or more times). In addition, parentheses are used to remember the first matched numeric characters.</p>
<p>This pattern is found in "Open Chapter 4.3, paragraph 6" and '4' is remembered. The pattern is not found in "Chapter 3 and 4", because that string does not have a period after the '3'.</p>
<p>To match a substring without causing the matched part to be remembered, within the parentheses preface the pattern with <code>?:</code>. For example, <code>(?:\d+)</code> matches one or more numeric characters but does not remember the matched characters.</p>
<h2 id="Lavorare_con_le_espressioni_regolari">Lavorare con le espressioni regolari</h2>
<p>Le espressioni regolari sono usate con i metodi <code>test</code> and <code>exec</code> di <code>RegExp</code> e con i metodi <code>match</code>, <code>replace</code>, <code>search</code>, and <code>split</code> di <code>String</code> .Questi metodi sono spiegati in dettaglio nelle <a href="/en-US/docs/Web/JavaScript/Reference" title="en-US/docs/JavaScript/Reference">JavaScript reference</a>.</p>
<p>Metodi che usano le espressioni regolari</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Metodo</th>
<th scope="col">Descrizione</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{jsxref("RegExp.exec", "exec")}}</td>
<td>
<p>Un metodo di <code>RegExp</code> che esegue una ricerca per una corrispondenza in una stringa. Ritorna un array di informazioni, o null se non trova corrispondenze.</p>
</td>
</tr>
<tr>
<td>{{jsxref("RegExp.test", "test")}}</td>
<td>Un metodo di <code>RegExp</code> che testa le corrispondenze in una stinga. Ritorna true o false. </td>
</tr>
<tr>
<td>{{jsxref("String.match", "match")}}</td>
<td>Un metodo di <code>String</code> che esegue una ricerca per una corrispondenza in una stringa. Ritorna un array di informazioni, o null se non trova corrispondenze.</td>
</tr>
<tr>
<td>{{jsxref("String.search", "search")}}</td>
<td>A <code>String</code> method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.</td>
</tr>
<tr>
<td>{{jsxref("String.replace", "replace")}}</td>
<td>A <code>String</code> method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.</td>
</tr>
<tr>
<td>{{jsxref("String.split", "split")}}</td>
<td>A <code>String</code> method that uses a regular expression or a fixed string to break a string into an array of substrings.</td>
</tr>
</tbody>
</table>
<p>When you want to know whether a pattern is found in a string, use the <code>test</code> or <code>search</code> method; for more information (but slower execution) use the <code>exec</code> or <code>match</code> methods. If you use <code>exec</code> or <code>match</code> and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, <code>RegExp</code>. If the match fails, the <code>exec</code> method returns <code>null</code> (which coerces to <code>false</code>).</p>
<p>In the following example, the script uses the <code>exec</code> method to find a match in a string.</p>
<pre class="brush: js">var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
</pre>
<p>If you do not need to access the properties of the regular expression, an alternative way of creating <code>myArray</code> is with this script:</p>
<pre class="brush: js">var myArray = /d(b+)d/g.exec("cdbbdbsbz"); // equivalent to "cdbbdbsbz".match(/d(b+)d/g);
</pre>
<p>If you want to construct the regular expression from a string, yet another alternative is this script:</p>
<pre class="brush: js">var myRe = new RegExp("d(b+)d", "g");
var myArray = myRe.exec("cdbbdbsbz");
</pre>
<p>With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.</p>
<table class="standard-table">
<caption>Results of regular expression execution.</caption>
<thead>
<tr>
<th scope="col">Object</th>
<th scope="col">Property or index</th>
<th scope="col">Description</th>
<th scope="col">In this example</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="4"><code>myArray</code></td>
<td></td>
<td>The matched string and all remembered substrings.</td>
<td><code>["dbbd", "bb"]</code></td>
</tr>
<tr>
<td><code>index</code></td>
<td>The 0-based index of the match in the input string.</td>
<td><code>1</code></td>
</tr>
<tr>
<td><code>input</code></td>
<td>The original string.</td>
<td><code>"cdbbdbsbz"</code></td>
</tr>
<tr>
<td><code>[0]</code></td>
<td>The last matched characters.</td>
<td><code>"dbbd"</code></td>
</tr>
<tr>
<td rowspan="2"><code>myRe</code></td>
<td><code>lastIndex</code></td>
<td>The index at which to start the next match. (This property is set only if the regular expression uses the g option, described in {{ web.link("#Advanced_searching_with_flags", "Advanced Searching With Flags") }}.)</td>
<td><code>5</code></td>
</tr>
<tr>
<td><code>source</code></td>
<td>The text of the pattern. Updated at the time that the regular expression is created, not executed.</td>
<td><code>"d(b+)d"</code></td>
</tr>
</tbody>
</table>
<p>As shown in the second form of this example, you can use a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:</p>
<pre class="brush: js">var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
console.log("The value of lastIndex is " + myRe.lastIndex);
// "The value of lastIndex is 5"
</pre>
<p>However, if you have this script:</p>
<pre class="brush: js">var myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log("The value of lastIndex is " + /d(b+)d/g.lastIndex);
// "The value of lastIndex is 0"
</pre>
<p>The occurrences of <code>/d(b+)d/g</code> in the two statements are different regular expression objects and hence have different values for their <code>lastIndex</code> property. If you need to access the properties of a regular expression created with an object initializer, you should first assign it to a variable.</p>
<h3 id="Using_parenthesized_substring_matches">Using parenthesized substring matches</h3>
<p>Including parentheses in a regular expression pattern causes the corresponding submatch to be remembered. For example, <code>/a(b)c/</code> matches the characters 'abc' and remembers 'b'. To recall these parenthesized substring matches, use the <code>Array</code> elements <code>[1]</code>, ..., <code>[n]</code>.</p>
<p>The number of possible parenthesized substrings is unlimited. The returned array holds all that were found. The following examples illustrate how to use parenthesized substring matches.</p>
<p>The following script uses the {{jsxref("String.replace", "replace()")}} method to switch the words in the string. For the replacement text, the script uses the <code>$1</code> and <code>$2</code> in the replacement to denote the first and second parenthesized substring matches.</p>
<pre class="brush: js">var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
</pre>
<p>This prints "Smith, John".</p>
<h3 id="Advanced_searching_with_flags">Advanced searching with flags</h3>
<p>Regular expressions have four optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any order, and are included as part of the regular expression.</p>
<table class="standard-table">
<caption>Regular expression flags</caption>
<thead>
<tr>
<th scope="col">Flag</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>g</code></td>
<td>Global search.</td>
</tr>
<tr>
<td>i</td>
<td>Case-insensitive search.</td>
</tr>
<tr>
<td>m</td>
<td>Multi-line search.</td>
</tr>
<tr>
<td>y</td>
<td>Perform a "sticky" search that matches starting at the current position in the target string. See {{jsxref("RegExp.sticky", "sticky")}}</td>
</tr>
</tbody>
</table>
<p>To include a flag with the regular expression, use this syntax:</p>
<pre class="brush: js">var re = /pattern/flags;
</pre>
<p>or</p>
<pre class="brush: js">var re = new RegExp("pattern", "flags");
</pre>
<p>Note that the flags are an integral part of a regular expression. They cannot be added or removed later.</p>
<p>For example, <code>re = /\w+\s/g</code> creates a regular expression that looks for one or more characters followed by a space, and it looks for this combination throughout the string.</p>
<pre class="brush: js">var re = /\w+\s/g;
var str = "fee fi fo fum";
var myArray = str.match(re);
console.log(myArray);
</pre>
<p>This displays ["fee ", "fi ", "fo "]. In this example, you could replace the line:</p>
<pre class="brush: js">var re = /\w+\s/g;
</pre>
<p>with:</p>
<pre class="brush: js">var re = new RegExp("\\w+\\s", "g");
</pre>
<p>and get the same result.</p>
<p>The <code>m</code> flag is used to specify that a multiline input string should be treated as multiple lines. If the <code>m</code> flag is used, <code>^</code> and <code>$</code> match at the start or end of any line within the input string instead of the start or end of the entire string.</p>
<h2 id="Examples">Examples</h2>
<p>The following examples show some uses of regular expressions.</p>
<h3 id="Changing_the_order_in_an_input_string">Changing the order in an input string</h3>
<p>The following example illustrates the formation of regular expressions and the use of <code>string.split()</code> and <code>string.replace()</code>. It cleans a roughly formatted input string containing names (first name first) separated by blanks, tabs and exactly one semicolon. Finally, it reverses the name order (last name first) and sorts the list.</p>
<pre class="brush: js">// The name string contains multiple spaces and tabs,
// and may have multiple spaces between first and last names.
var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var output = ["---------- Original String\n", names + "\n"];
// Prepare two regular expression patterns and array storage.
// Split the string into array elements.
// pattern: possible white space then semicolon then possible white space
var pattern = /\s*;\s*/;
// Break the string into pieces separated by the pattern above and
// store the pieces in an array called nameList
var nameList = names.split(pattern);
// new pattern: one or more characters then spaces then characters.
// Use parentheses to "memorize" portions of the pattern.
// The memorized portions are referred to later.
pattern = /(\w+)\s+(\w+)/;
// New array for holding names being processed.
var bySurnameList = [];
// Display the name array and populate the new array
// with comma-separated names, last first.
//
// The replace method removes anything matching the pattern
// and replaces it with the memorized string—second memorized portion
// followed by comma space followed by first memorized portion.
//
// The variables $1 and $2 refer to the portions
// memorized while matching the pattern.
output.push("---------- After Split by Regular Expression");
var i, len;
for (i = 0, len = nameList.length; i < len; i++){
output.push(nameList[i]);
bySurnameList[i] = nameList[i].replace(pattern, "$2, $1");
}
// Display the new array.
output.push("---------- Names Reversed");
for (i = 0, len = bySurnameList.length; i < len; i++){
output.push(bySurnameList[i]);
}
// Sort by last name, then display the sorted array.
bySurnameList.sort();
output.push("---------- Sorted");
for (i = 0, len = bySurnameList.length; i < len; i++){
output.push(bySurnameList[i]);
}
output.push("---------- End");
console.log(output.join("\n"));
</pre>
<h3 id="Using_special_characters_to_verify_input">Using special characters to verify input</h3>
<p>In the following example, the user is expected to enter a phone number. When the user presses the "Check" button, the script checks the validity of the number. If the number is valid (matches the character sequence specified by the regular expression), the script shows a message thanking the user and confirming the number. If the number is invalid, the script informs the user that the phone number is not valid.</p>
<p>Within non-capturing parentheses <code>(?:</code> , the regular expression looks for three numeric characters <code>\d{3}</code> OR <code>|</code> a left parenthesis <code>\(</code> followed by three digits<code> \d{3}</code>, followed by a close parenthesis <code>\)</code>, (end non-capturing parenthesis <code>)</code>), followed by one dash, forward slash, or decimal point and when found, remember the character <code>([-\/\.])</code>, followed by three digits <code>\d{3}</code>, followed by the remembered match of a dash, forward slash, or decimal point <code>\1</code>, followed by four digits <code>\d{4}</code>.</p>
<p>The <code>Change</code> event activated when the user presses Enter sets the value of <code>RegExp.input</code>.</p>
<pre class="brush: html"><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/;
function testInfo(phoneInput){
var OK = re.exec(phoneInput.value);
if (!OK)
window.alert(phoneInput.value + " isn't a phone number with area code!");
else
window.alert("Thanks, your phone number is " + OK[0]);
}
</script>
</head>
<body>
<p>Enter your phone number (with area code) and then click "Check".
<br>The expected format is like ###-###-####.</p>
<form action="#">
<input id="phone"><button onclick="testInfo(document.getElementById('phone'));">Check</button>
</form>
</body>
</html>
</pre>
<div>{{PreviousNext("Web/JavaScript/Guide/Text_formatting", "Web/JavaScript/Guide/Indexed_collections")}}</div>
|