Skip to content

Commit 2acbfc4

Browse files
authored
Merge pull request #102 from DavideDaSerra/master
Quantifiers article and exercises translated to Italian
2 parents 8f90bd4 + b4a8450 commit 2acbfc4

File tree

6 files changed

+108
-111
lines changed

6 files changed

+108
-111
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
Solution:
2+
Soluzione:
33

44
```js run
55
let reg = /\.{3,}/g;
6-
alert( "Hello!... How goes?.....".match(reg) ); // ..., .....
6+
alert( "Ciao!... Come va?.....".match(reg) ); // ..., .....
77
```
88

9-
Please note that the dot is a special character, so we have to escape it and insert as `\.`.
9+
Si noti che il punto è un carattere speciale pertanto è stato necessario farne l'escape inserendolo come `\.`.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
importance: 5
1+
Importanza: 5
22

33
---
44

5-
# How to find an ellipsis "..." ?
5+
# Come trovare i puntini di sospensione "..." ?
66

7-
Create a regexp to find ellipsis: 3 (or more?) dots in a row.
7+
Si crei un'espressione regolare che individui i puntini di sospensione: 3 (o più) punti in fila
88

9-
Check it:
9+
Prova:
1010

1111
```js
12-
let reg = /your regexp/g;
13-
alert( "Hello!... How goes?.....".match(reg) ); // ..., .....
12+
let reg = /la tua regex/g;
13+
alert( "Ciao!... come va?.....".match(reg) ); // ..., .....
1414
```
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
We need to look for `#` followed by 6 hexadecimal characters.
1+
Dobbiamo cercare il carattere `#` seguito da 6 cifre esadecimalis.
22

3-
A hexadecimal character can be described as `pattern:[0-9a-fA-F]`. Or if we use the `i` flag, then just `pattern:[0-9a-f]`.
3+
Possiamo descrivere una cifra esadecimale con il `pattern:[0-9a-fA-F]`, oppure, usando il flag `i` possiamo usare `pattern:[0-9a-f]`.
44

5-
Then we can look for 6 of them using the quantifier `pattern:{6}`.
5+
Dunque si cerchino 6 pattern usando il quantificatore `pattern:{6}`.
66

7-
As a result, we have the regexp: `pattern:/#[a-f0-9]{6}/gi`.
7+
Il risultato sarà l'espressione regolare: `pattern:/#[a-f0-9]{6}/gi`.
88

99
```js run
1010
let reg = /#[a-f0-9]{6}/gi;
@@ -14,18 +14,18 @@ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2"
1414
alert( str.match(reg) ); // #121212,#AA00ef
1515
```
1616

17-
The problem is that it finds the color in longer sequences:
17+
Il problema è che così si trova il colore anche in sequenze più lunghe di 6 caratteri:
1818

1919
```js run
2020
alert( "#12345678".match( /#[a-f0-9]{6}/gi ) ) // #12345678
2121
```
2222

23-
To fix that, we can add `pattern:\b` to the end:
23+
Per sisolvere tale problema si può aggiungere `pattern:\b` in coda all'espressione:
2424

2525
```js run
26-
// color
26+
// Un valore corrispondente colore
2727
alert( "#123456".match( /#[a-f0-9]{6}\b/gi ) ); // #123456
2828

29-
// not a color
29+
// Un valore non corrispondente a un colore
3030
alert( "#12345678".match( /#[a-f0-9]{6}\b/gi ) ); // null
3131
```
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Regexp for HTML colors
1+
# Regexp per la ricerca di colori espressi in formato HTML
22

3-
Create a regexp to search HTML-colors written as `#ABCDEF`: first `#` and then 6 hexadecimal characters.
3+
Si crei un'espressione regolare per trovare i colori HTML in notazione `#ABCDEF`: all'inizio un carattere `#` quindi 6 cifre esadecimali.
44

5-
An example of use:
5+
Un esempio di uso:
66

77
```js
8-
let reg = /...your regexp.../
8+
let reg = /...la tua regexp.../
99

1010
let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2 #12345678";
1111

1212
alert( str.match(reg) ) // #121212,#AA00ef
1313
```
1414

15-
P.S. In this task we do not need other color formats like `#123` or `rgb(1,2,3)` etc.
15+
P.S. In questo esercizio non è richiesto di intercettare i colori espressi anche in altri formati come: `#123` o `rgb(1,2,3)` ecc.
Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
1-
# Quantifiers +, *, ? and {n}
1+
# Quantificatori +, *, ? and {n}
22

3-
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`.
44

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*.
66

7-
## Quantity {n}
7+
## Quantità {n}
88

9-
The simplest quantifier is a number in curly braces: `pattern:{n}`.
9+
Il quantificatore più semplice è un numero tra parentesi graffe `pattern:{n}`.
1010

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:
1213

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`.
1916

17+
Nell'esempio sottostante si cerca un numero a 5 cifre:
18+
2019
```js run
21-
alert( "I'm 12345 years old".match(/\d{5}/) ); // "12345"
20+
alert( "Ho 12345 anni".match(/\d{5}/) ); // "12345"
2221
```
2322

24-
We can add `\b` to exclude longer numbers: `pattern:\b\d{5}\b`.
23+
Si può aggiungere `\b` per escludere i numeri più lunghi: `pattern:\b\d{5}\b`.
2524

26-
The range: `{3,5}`, match 3-5 times
27-
: To find numbers from 3 to 5 digits we can put the limits into curly braces: `pattern:\d{3,5}`
25+
L'intervallo: `{3,5}`, trova da 3 a 5 ripetizioni.
26+
: Per trovare i numeri da 3 a 5 cifre si possono mettere i limiti tra parentesi graffe: `pattern:\d{3,5}`
2827

2928
```js run
30-
alert( "I'm not 12, but 1234 years old".match(/\d{3,5}/) ); // "1234"
29+
alert( "Non ho 12 anni 12, ma 1234".match(/\d{3,5}/) ); // "1234"
3130
```
3231

33-
We can omit the upper limit.
32+
Si può tralasciare il limite superiore.
3433

35-
Then a regexp `pattern:\d{3,}` looks for sequences of digits of length `3` or more:
34+
In questo caso la regexp `pattern:\d{3,}` cercherà successioni di cifre lunghe `3` o più:
3635

3736
```js run
38-
alert( "I'm not 12, but 345678 years old".match(/\d{3,}/) ); // "345678"
37+
alert( "Non ho 12 anni, ma 345678".match(/\d{3,}/) ); // "345678"
3938
```
4039

41-
Let's return to the string `+7(903)-123-45-67`.
40+
Torniamo alla stringa: `+7(903)-123-45-67`.
4241

43-
A number is a sequence of one or more digits in a row. So the regexp is `pattern:\d{1,}`:
42+
Un numero è una sequenza di una o più cifre in successione, dunque l'espressione regolare sarà `pattern:\d{1,}`:
4443

4544
```js run
4645
let str = "+7(903)-123-45-67";
4746

48-
let numbers = str.match(/\d{1,}/g);
47+
let numeri = str.match(/\d{1,}/g);
4948

50-
alert(numbers); // 7,903,123,45,67
49+
alert(numeri); // 7,903,123,45,67
5150
```
5251

53-
## Shorthands
52+
## Abbreviazioni
5453

55-
There are shorthands for most used quantifiers:
54+
Sono presenti abbreviazioni per i quantificatori di uso più comune:
5655

5756
`+`
58-
: Means "one or more", the same as `{1,}`.
57+
: Significa "uno o più", identico a `{1,}`.
5958

60-
For instance, `pattern:\d+` looks for numbers:
59+
Ad esempio, `pattern:\d+` cerca i numeri:
6160

6261
```js run
6362
let str = "+7(903)-123-45-67";
@@ -66,75 +65,75 @@ There are shorthands for most used quantifiers:
6665
```
6766

6867
`?`
69-
: Means "zero or one", the same as `{0,1}`. In other words, it makes the symbol optional.
68+
: Significa "zero o uno" identico a `{0,1}`. In altri termini rende il simbolo opzionale.
7069

71-
For instance, the pattern `pattern:ou?r` looks for `match:o` followed by zero or one `match:u`, and then `match:r`.
70+
Ad esempio, il pattern `pattern:ou?r` cerca `match:o` seguito opzionalmente da `match:u`, quindi da `match:r`.
7271

73-
So, `pattern:colou?r` finds both `match:color` and `match:colour`:
72+
Dunque, `pattern:colou?r` trova sia `match:color` che `match:colour`:
7473

7574
```js run
76-
let str = "Should I write color or colour?";
75+
let str = "In Inglese \"colore\" siscrive sia \"color\" che \"colour\"?";
7776

7877
alert( str.match(/colou?r/g) ); // color, colour
7978
```
8079

8180
`*`
82-
: Means "zero or more", the same as `{0,}`. That is, the character may repeat any times or be absent.
81+
: Significa "zero o più", lo stesso che `{0,}`. Significa che il carattere può esserci, essere ripetuto o mancare.
8382

84-
For example, `pattern:\d0*` looks for a digit followed by any number of zeroes:
83+
Ad esempio, `pattern:\d0*` cerca una cifra seguita da un numero arbitrario di zeri:
8584

8685
```js run
8786
alert( "100 10 1".match(/\d0*/g) ); // 100, 10, 1
8887
```
8988

90-
Compare it with `'+'` (one or more):
89+
Confronto con `'+'` (uno o più):
9190

9291
```js run
9392
alert( "100 10 1".match(/\d0+/g) ); // 100, 10
94-
// 1 not matched, as 0+ requires at least one zero
93+
// 1 non compare, poichè 0+ richiede almeno uno 0
9594
```
9695

97-
## More examples
96+
## Altri esempi
9897

99-
Quantifiers are used very often. They serve as the main "building block" of complex regular expressions, so let's see more examples.
98+
I quantificatori si usano spesso; essi costituiscono i blocchi principali per espressioni regolari complesse, si riportano in seguito altri esempi.
10099

101-
Regexp "decimal fraction" (a number with a floating point): `pattern:\d+\.\d+`
102-
: In action:
100+
Regexp "frazione decimale" (un numero con la virgola): `pattern:\d+,\d+`
101+
: In funzione:
103102
```js run
104-
alert( "0 1 12.345 7890".match(/\d+\.\d+/g) ); // 12.345
103+
alert( "0 1 12,345 7890".match(/\d+,\d+/g) ); // 12,345
105104
```
106105

107-
Regexp "open HTML-tag without attributes", like `<span>` or `<p>`: `pattern:/<[a-z]+>/i`
108-
: In action:
106+
Regexp "Apertura di un tag HTML senza attributi" come, ad esempio, `<span>` oppure `<p>`: `pattern:/<[a-z]+>/i`
107+
: In funzione:
109108

110109
```js run
111110
alert( "<body> ... </body>".match(/<[a-z]+>/gi) ); // <body>
112111
```
113112

114-
We look for character `pattern:'<'` followed by one or more Latin letters, and then `pattern:'>'`.
113+
Cerchiamo il carattere `pattern:'<'` seguito da una o più lettere latine one or more Latin letters, quindi `pattern:'>'`.
115114

116-
Regexp "open HTML-tag without attributes" (improved): `pattern:/<[a-z][a-z0-9]*>/i`
117-
: Better regexp: according to the standard, HTML tag name may have a digit at any position except the first one, like `<h1>`.
115+
Regexp "Apertura di un tag HTML senza attributi" (migliorato): `pattern:/<[a-z][a-z0-9]*>/i`
116+
: Espressione regolare migliore: secondo lo standard, il nome di un tag HTML può avere cifre in qualsiasi posizione tranne la prima `<h1>`.
118117

119118
```js run
120119
alert( "<h1>Hi!</h1>".match(/<[a-z][a-z0-9]*>/gi) ); // <h1>
121120
```
122121

123-
Regexp "opening or closing HTML-tag without attributes": `pattern:/<\/?[a-z][a-z0-9]*>/i`
124-
: We added an optional slash `pattern:/?` before the tag. Had to escape it with a backslash, otherwise JavaScript would think it is the pattern end.
122+
Regexp "Apertura o chiusura di un tag HTML senza attributi": `pattern:/<\/?[a-z][a-z0-9]*>/i`
123+
: Aggiungiamo una slash opzionale `pattern:/?` prima del nome tag, dobbiamo farne l'escap altrimenti JavaScript penserebbe che sia la fine del pattern.
125124

126125
```js run
127126
alert( "<h1>Hi!</h1>".match(/<\/?[a-z][a-z0-9]*>/gi) ); // <h1>, </h1>
128127
```
129128

130-
```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.
132131
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+>`.
134133
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.
136135
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]*>`?
138137
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.
140139
```
Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,73 @@
1-
# Multiline mode, flag "m"
1+
# Modo multilinea, flag "m"
2+
Il modo multilinea si abilita col flag `pattern:/.../m`.
23

3-
The multiline mode is enabled by the flag `pattern:/.../m`.
4+
Esso modifica il comportamento dei soli `pattern:^` e `pattern:$`.
45

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.
67

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 ^
89

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:
1211

1312
```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`;
1716

1817
*!*
1918
alert( str.match(/^\d+/gm) ); // 1, 2, 33
2019
*/!*
2120
```
2221

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:
2624

2725
```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`;
3129

3230
*!*
3331
alert( str.match(/^\d+/g) ); // 1
3432
*/!*
3533
```
3634

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.
3836

39-
## Line end $
37+
## Fine linea $
4038

41-
The dollar sign `pattern:$` behaves similarly.
39+
Il simbolo di dollaro `pattern:$` si comporta in modo simile.
4240

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.
4442

4543
```js run
46-
let str = `1st place: Winnie
47-
2nd place: Piglet
48-
33rd place: Eeyore`;
44+
let str = `1o posto: Winnie
45+
2o posto: Piglet
46+
33o posto: Eeyore`;
4947

5048
alert( str.match(/\w+$/gim) ); // Winnie,Piglet,Eeyore
5149
```
5250

53-
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.
5452

55-
## Anchors ^$ versus \n
53+
## Ancore ^$ rispetto \n (a-capo)
5654

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`.
5856

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.
6058

61-
For instance, here we use it instead of `pattern:$`:
59+
Ad esempio qui lo usiamo al posto di `pattern:$`:
6260

6361
```js run
64-
let str = `1st place: Winnie
65-
2nd place: Piglet
66-
33rd place: Eeyore`;
67-
62+
let str = `1o posto: Winnie
63+
2o posto: Piglet
64+
33o posto: Eeyore`;
6865
alert( str.match(/\w+\n/gim) ); // Winnie\n,Piglet\n
6966
```
7067

71-
Here every match is a word plus a newline character.
68+
Qui ogni corrispondenza è una parola più il carattere a-capo.
69+
70+
Un'altra differenza -- l' a-capo `\n` non trova corrispondenza a fine stringa. Questo è il motivo per cui `Eeyore` non è trovato nell'esempio sopra.
7271

73-
And one more difference -- the newline `\n` does not match at the string end. That's why `Eeyore` is not found in the example above.
72+
Pertanto le ancore sono migliori: sono più vicine a ciò che si vuole estrarre.
7473

75-
So, anchors are usually better, they are closer to what we want to get.

0 commit comments

Comments
 (0)