Skip to content

Commit 600d5fd

Browse files
committed
Tradotto articolo
1 parent 110e178 commit 600d5fd

File tree

3 files changed

+66
-67
lines changed

3 files changed

+66
-67
lines changed
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
```

0 commit comments

Comments
 (0)