You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's say we have a string like `+7(903)-123-45-67`and want to find all numbers in it. But unlike before, we are interested not in single digits, but full numbers: `7, 903, 123, 45, 67`.
3
+
Se si ha una stringa come la seguente: `+7(903)-123-45-67`e si vogliono trovare tutti i numeri che contiene ma, diversamente da prima, non siamo interessati alle singole cifre bensì agli interi numeri: `7, 903, 123, 45, 67`.
4
4
5
-
A number is a sequence of 1 or more digits`\d`. To mark how many we need, we need to append a *quantifier*.
5
+
Se definisce numero un sequenza di una o più cifre`\d`, per marcare quanti ne servono si deve aggiungere in coda un *quantificatore*.
6
6
7
-
## Quantity {n}
7
+
## Quantità {n}
8
8
9
-
The simplest quantifier is a number in curly braces:`pattern:{n}`.
9
+
Il quantificatore più semplice è un numero tra parentesi graffe`pattern:{n}`.
10
10
11
-
A quantifier is appended to a character (or a character class, or a `[...]` set etc) and specifies how many we need.
11
+
Un quantificatore si appende a un carattere (o a una classe di caratteri `[...]` un set, ecc..) specificando quante volte questo debba essere ripetuto.
12
+
Si riportano alcune forme avanzate, eccone alcuni esempi:
12
13
13
-
It has a few advanced forms, let's see examples:
14
-
15
-
The exact count: `{5}`
16
-
: `pattern:\d{5}` denotes exactly 5 digits, the same as `pattern:\d\d\d\d\d`.
17
-
18
-
The example below looks for a 5-digit number:
14
+
Il conteggio esatto: `{5}`
15
+
: `pattern:\d{5}` denota esattamente 5 cifre, identico a `pattern:\d\d\d\d\d`.
19
16
17
+
Nell'esempio sottostante si cerca un numero a 5 cifre:
18
+
20
19
```js run
21
-
alert( "I'm 12345 years old".match(/\d{5}/) ); // "12345"
```smart header="To make a regexp more precise, we often need make it more complex"
131
-
We can see one common rule in these examples: the more precise is the regular expression -- the longer and more complex it is.
129
+
```smart header="Per rendere una espressione regolare più precisa, spesso dobbiamo renderla anche più complessa"
130
+
Possiamo vederne una regola comune in questo esempio: più l'espressione regolare è precisa -- più è lunga e complicata.
132
131
133
-
For instance, for HTML tags we could use a simpler regexp: `pattern:<\w+>`.
132
+
Ad esempio, per i tag HTML è possibile usare un'espressione regolare più semplice: `pattern:<\w+>`.
134
133
135
-
...But because `pattern:\w` means any Latin letter or a digit or `'_'`, the regexp also matches non-tags, for instance `match:<_>`. So it's much simpler than `pattern:<[a-z][a-z0-9]*>`, but less reliable.
134
+
...Ma poiché `pattern:\w` intercetta qualsiasi lettera dell'alfabeto latino e il carattere `'_'`, la regexp intercetta anche dei non-tag, come `match:<_>`. Dunque è molto più semplice del pattern `pattern:<[a-z][a-z0-9]*>`, ma anche meno affidabile.
136
135
137
-
Are we ok with `pattern:<\w+>` or we need `pattern:<[a-z][a-z0-9]*>`?
136
+
Basta accontentarsi di `pattern:<\w+>` o è necessario `pattern:<[a-z][a-z0-9]*>`?
138
137
139
-
In real life both variants are acceptable. Depends on how tolerant we can be to "extra" matches and whether it's difficult or not to filter them out by other means.
138
+
La risposta è che nella vita reale entrambi sono accettabili; dipende però dal bilanciamento tra l'avere molte corrispondenze "extra" da filtrare in altri modi rispetto all'avere un'erspressione regolare più complessa.
Il modo multilinea si abilita col flag `pattern:/.../m`.
2
3
3
-
The multiline mode is enabled by the flag`pattern:/.../m`.
4
+
Esso modifica il comportamento dei soli `pattern:^` e`pattern:$`.
4
5
5
-
It only affects the behavior of `pattern:^` and `pattern:$`.
6
+
In modalità multilinea essi non trovano corrispondenza solo con l'inizio e la fine della stringa ma anche con gli inizi e fine di linea.
6
7
7
-
In the multiline mode they match not only at the beginning and end of the string, but also at start/end of line.
8
+
## Inizio linea ^
8
9
9
-
## Line start ^
10
-
11
-
In the example below the text has multiple lines. The pattern `pattern:/^\d+/gm` takes a number from the beginning of each one:
10
+
Nell'esempio sotto riportato il testo è distribuito su più linee. Il pattern `pattern:/^\d+/gm` prende un numero dall'inizio di ogni riga:
12
11
13
12
```js run
14
-
let str =`1st place: Winnie
15
-
2nd place: Piglet
16
-
33rd place: Eeyore`;
13
+
let str =`1o posto: Winnie
14
+
2o posto: Piglet
15
+
33o posto: Eeyore`;
17
16
18
17
*!*
19
18
alert( str.match(/^\d+/gm) ); // 1, 2, 33
20
19
*/!*
21
20
```
22
21
23
-
The regexp engine moves along the text and looks for a line start `pattern:^`, when finds -- continues to match the rest of the pattern `pattern:\d+`.
24
-
25
-
Without the flag `pattern:/.../m` only the first number is matched:
22
+
Il motore di regex si sposta sul testo alla ricerca di un inizio riga `pattern:^`, quando lo trova -- continua a trovare la corrispondenza col resto del pattern `pattern:\d+`.
23
+
Mentre senza il flag `pattern:/.../m` troverebbe corrispondenza solo col primo numero:
26
24
27
25
```js run
28
-
let str =`1st place: Winnie
29
-
2nd place: Piglet
30
-
33rd place: Eeyore`;
26
+
let str =`1o posto: Winnie
27
+
2o posto: Piglet
28
+
33o posto: Eeyore`;
31
29
32
30
*!*
33
31
alert( str.match(/^\d+/g) ); // 1
34
32
*/!*
35
33
```
36
34
37
-
That's because by default a caret `pattern:^`only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
35
+
Questo accade perchè di base il comportamento del caret `pattern:^`trova corrispondenza solo con l'inizio del testo, mentre in modo multilinea trova -- con l'inizio di ogni riga.
38
36
39
-
## Line end $
37
+
## Fine linea $
40
38
41
-
The dollar sign `pattern:$`behaves similarly.
39
+
Il simbolo di dollaro `pattern:$`si comporta in modo simile.
42
40
43
-
The regular expression `pattern:\w+$`finds the last word in every line
41
+
L'espressione regolare `pattern:\w+$`trova l'ultima parola in ogni stringa.
Without the `pattern:/.../m`flag the dollar `pattern:$`would only match the end of the whole string, so only the very last word would be found.
51
+
Senza il flag `pattern:/.../m`il dollaro `pattern:$`troverebbe corrispondenza solo con la fine dell'intera stringa, dunque risulterebbe solo l'ultima parola dell'intero testo.
54
52
55
-
## Anchors ^$ versus \n
53
+
## Ancore ^$ rispetto \n (a-capo)
56
54
57
-
To find a newline, we can use not only `pattern:^`and`pattern:$`, but also the newline character`\n`.
55
+
Per trovare un fine linea è possibile usare non solo `pattern:^`e`pattern:$`, ma anche il carattere a-capo`\n`.
58
56
59
-
The first difference is that unlike anchors, the character`\n` "consumes" the newline character and adds it to the result.
57
+
La prima differenza è che, contrariamente alle ancore, il carattere`\n` "consuma" il carattere a-capo aggiungendolo al risultato.
60
58
61
-
For instance, here we use it instead of`pattern:$`:
0 commit comments