|
1 | | -# Sets and ranges [...] |
| 1 | +# Insiemi e intervalli [...] |
2 | 2 |
|
3 | | -Several characters or character classes inside square brackets `[β¦]` mean to "search for any character among given". |
| 3 | +Alcuni caratteri o classi di caratteri inseriti all'interno di parantesi quadre `[β¦]` significano "cerca qualsiasi carattere tra quelli forniti". |
4 | 4 |
|
5 | | -## Sets |
| 5 | +## Insiemi |
6 | 6 |
|
7 | | -For instance, `pattern:[eao]` means any of the 3 characters: `'a'`, `'e'`, or `'o'`. |
| 7 | +Per esempio, `pattern:[eao]` significa uno qualunque dei 3 caratteri: `'a'`, `'e'`, od `'o'`. |
8 | 8 |
|
9 | | -That's called a *set*. Sets can be used in a regexp along with regular characters: |
| 9 | +Questo Γ¨ chiamato un *insieme* o *set*. I set posso essere usati in una regexp insieme ad altri caratteri: |
10 | 10 |
|
11 | 11 | ```js run |
12 | | -// find [t or m], and then "op" |
| 12 | +// trova [t o m], e quindi "op" |
13 | 13 | alert( "Mop top".match(/[tm]op/gi) ); // "Mop", "top" |
14 | 14 | ``` |
15 | 15 |
|
16 | | -Please note that although there are multiple characters in the set, they correspond to exactly one character in the match. |
| 16 | +Si noti che sebbene ci siano piΓΉ caratteri nel set, questi corrispondano esattamente a un carattere nel match. |
17 | 17 |
|
18 | | -So the example below gives no matches: |
| 18 | +Quindi il seguente esempio non dΓ alcuna corrispondenza: |
19 | 19 |
|
20 | 20 | ```js run |
21 | | -// find "V", then [o or i], then "la" |
22 | | -alert( "Voila".match(/V[oi]la/) ); // null, no matches |
| 21 | +// trova "V", poi ['o' o 'i'], quindi "la" |
| 22 | +alert( "Voila".match(/V[oi]la/) ); // null, nessuna corrispondenza |
23 | 23 | ``` |
24 | 24 |
|
25 | | -The pattern searches for: |
| 25 | +Il modello di ricerca risulta quindi: |
26 | 26 |
|
27 | 27 | - `pattern:V`, |
28 | | -- then *one* of the letters `pattern:[oi]`, |
29 | | -- then `pattern:la`. |
| 28 | +- poi *una* di queste lettere `pattern:[oi]`, |
| 29 | +- quindi `pattern:la`. |
30 | 30 |
|
31 | | -So there would be a match for `match:Vola` or `match:Vila`. |
| 31 | +Significa che ci dovrebbe essere una corrispondenza per `match:Vola` o `match:Vila`. |
32 | 32 |
|
33 | | -## Ranges |
| 33 | +## Intervalli |
34 | 34 |
|
35 | | -Square brackets may also contain *character ranges*. |
| 35 | +Le parentesi quadre possono contenere anche *intervalli di caratteri*. |
36 | 36 |
|
37 | | -For instance, `pattern:[a-z]` is a character in range from `a` to `z`, and `pattern:[0-5]` is a digit from `0` to `5`. |
| 37 | +Per esempio, `pattern:[a-z]` indica un carattere nell'intervallo che va da `a` a `z`, e `pattern:[0-5]` indica un numero tra `0` e `5`. |
38 | 38 |
|
39 | | -In the example below we're searching for `"x"` followed by two digits or letters from `A` to `F`: |
| 39 | +Nell'esempio seguente cercheremo una `"x"` seguita da due numeri o lettere da `A` a `F`: |
40 | 40 |
|
41 | 41 | ```js run |
42 | 42 | alert( "Exception 0xAF".match(/x[0-9A-F][0-9A-F]/g) ); // xAF |
43 | 43 | ``` |
44 | 44 |
|
45 | | -Here `pattern:[0-9A-F]` has two ranges: it searches for a character that is either a digit from `0` to `9` or a letter from `A` to `F`. |
| 45 | +Il modello `pattern:[0-9A-F]` ha due intervalli: cerca un carattere che sia una cifra da `0` a `9` o una lettera da `A` a `F`. |
46 | 46 |
|
47 | | -If we'd like to look for lowercase letters as well, we can add the range `a-f`: `pattern:[0-9A-Fa-f]`. Or add the flag `pattern:i`. |
| 47 | +Se volessimo cercare anche lettere minuscole, possiamo aggiungere l'intervallo `a-f`: `pattern:[0-9A-Fa-f]`, o aggiungere il flag `pattern:i`. |
48 | 48 |
|
49 | | -We can also use character classes inside `[β¦]`. |
| 49 | +Possiamo anche usare classi di caratteri dentro `[β¦]`. |
50 | 50 |
|
51 | | -For instance, if we'd like to look for a wordly character `pattern:\w` or a hyphen `pattern:-`, then the set is `pattern:[\w-]`. |
| 51 | +Per esempio, se volessimo cercare un carattere di parola `pattern:\w` o un trattino `pattern:-`, allora l'insieme sarΓ `pattern:[\w-]`. |
52 | 52 |
|
53 | | -Combining multiple classes is also possible, e.g. `pattern:[\s\d]` means "a space character or a digit". |
| 53 | +Γ anche possibile combinare diverse classi, es `pattern:[\s\d]` significa "uno spazio o un numero". |
54 | 54 |
|
55 | | -```smart header="Character classes are shorthands for certain character sets" |
56 | | -For instance: |
| 55 | +```smart header="Le classi di caratteri sono abbreviazioni per determinati set di caratteri" |
| 56 | +Per esempio: |
57 | 57 |
|
58 | | -- **\d** -- is the same as `pattern:[0-9]`, |
59 | | -- **\w** -- is the same as `pattern:[a-zA-Z0-9_]`, |
60 | | -- **\s** -- is the same as `pattern:[\t\n\v\f\r ]`, plus few other rare Unicode space characters. |
| 58 | +- **\d** -- Γ¨ la stessa cosa di `pattern:[0-9]`, |
| 59 | +- **\w** -- Γ¨ la stessa cosa di `pattern:[a-zA-Z0-9_]`, |
| 60 | +- **\s** -- Γ¨ la stessa cosa di `pattern:[\t\n\v\f\r ]` e pochi altri rari caratteri Unicode. |
61 | 61 | ``` |
62 | 62 |
|
63 | | -### Example: multi-language \w |
| 63 | +### Esempio: multi lingua \w |
64 | 64 |
|
65 | | -As the character class `pattern:\w` is a shorthand for `pattern:[a-zA-Z0-9_]`, it can't find Chinese hieroglyphs, Cyrillic letters, etc. |
| 65 | +Dal momento che la classe di caratteri `pattern:\w` Γ¨ una scorciatoia per `pattern:[a-zA-Z0-9_]`, non puΓ² trovare geroglifici cinesi, lettere cirilliche, ecc. |
66 | 66 |
|
67 | | -We can write a more universal pattern, that looks for wordly characters in any language. That's easy with Unicode properties: `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`. |
| 67 | +Possiamo allora scrivere un modello piΓΉ universale, che cerca un carattere di parola in qualunque lingua. Questo Γ¨ reso facile dalle proprietΓ Unicode: `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`. |
68 | 68 |
|
69 | | -Let's decipher it. Similar to `pattern:\w`, we're making a set of our own that includes characters with following Unicode properties: |
| 69 | +Decifriamolo. Similarmente a `pattern:\w`, stiamo creando un nostro insieme che include i caratteri con le seguenti proprietΓ Unicode: |
70 | 70 |
|
71 | | -- `Alphabetic` (`Alpha`) - for letters, |
72 | | -- `Mark` (`M`) - for accents, |
73 | | -- `Decimal_Number` (`Nd`) - for digits, |
74 | | -- `Connector_Punctuation` (`Pc`) - for the underscore `'_'` and similar characters, |
75 | | -- `Join_Control` (`Join_C`) - two special codes `200c` and `200d`, used in ligatures, e.g. in Arabic. |
| 71 | +- `Alphabetic` (`Alpha`) - per le lettere, |
| 72 | +- `Mark` (`M`) - per gli accenti, |
| 73 | +- `Decimal_Number` (`Nd`) - per i numeri, |
| 74 | +- `Connector_Punctuation` (`Pc`) - per il trattino basso `'_'` e caratteri simili, |
| 75 | +- `Join_Control` (`Join_C`) - due codici speciali `200c` e `200d`, usati nelle legature, a.e. in Arabo. |
76 | 76 |
|
77 | | -An example of use: |
| 77 | +Un esempio di utilizzo: |
78 | 78 |
|
79 | 79 | ```js run |
80 | 80 | let regexp = /[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]/gu; |
81 | 81 |
|
82 | 82 | let str = `Hi δ½ ε₯½ 12`; |
83 | 83 |
|
84 | | -// finds all letters and digits: |
| 84 | +// Trova tutte le lettere e i numeri: |
85 | 85 | alert( str.match(regexp) ); // H,i,δ½ ,ε₯½,1,2 |
86 | 86 | ``` |
87 | 87 |
|
88 | | -Of course, we can edit this pattern: add Unicode properties or remove them. Unicode properties are covered in more details in the article <info:regexp-unicode>. |
| 88 | +Naturalmente possiamo modificare questo modello: aggiungere proprietΓ Unicode o rimuoverle. Le proprietΓ Unicode sono descritte meglio nell'articolo <info:regexp-unicode>. |
89 | 89 |
|
90 | | -```warn header="Unicode properties aren't supported in IE" |
91 | | -Unicode properties `pattern:p{β¦}` are not implemented in IE. If we really need them, we can use library [XRegExp](http://xregexp.com/). |
| 90 | +```warn header="Le proprietΓ Unicode non sono supportate da IE" |
| 91 | +Le proprietΓ Unicode `pattern:p{β¦}` non sono implementate in IE. Se ne abbiamo davvero bisogno possiamo utilizzare la libreria [XRegExp](http://xregexp.com/). |
92 | 92 |
|
93 | | -Or just use ranges of characters in a language that interests us, e.g. `pattern:[Π°-Ρ]` for Cyrillic letters. |
| 93 | +In alternativa possiamo utilizzare soltanto un intervallo di caratteri nella lingua che ci interessa, a.e. `pattern:[Π°-Ρ]` per le lettere cirilliche. |
94 | 94 | ``` |
95 | 95 |
|
96 | | -## Excluding ranges |
| 96 | +## Esclusione di intervalli |
97 | 97 |
|
98 | | -Besides normal ranges, there are "excluding" ranges that look like `pattern:[^β¦]`. |
| 98 | +Oltre ai normali intervalli, Γ¨ possibile creare dei modelli di "esclusione", come `pattern:[^β¦]`. |
99 | 99 |
|
100 | | -They are denoted by a caret character `^` at the start and match any character *except the given ones*. |
| 100 | +Sono contraddistinti da un accento circonflesso `^` all'inizio e trovano corrispondenza in qualunque carattere *tranne quelli indicati*. |
101 | 101 |
|
102 | | -For instance: |
| 102 | +Per esempio: |
103 | 103 |
|
104 | | -- `pattern:[^aeyo]` -- any character except `'a'`, `'e'`, `'y'` or `'o'`. |
105 | | -- `pattern:[^0-9]` -- any character except a digit, the same as `pattern:\D`. |
106 | | -- `pattern:[^\s]` -- any non-space character, same as `\S`. |
| 104 | +- `pattern:[^aeyo]` -- qualunque carattere tranne `'a'`, `'e'`, `'y'` o `'o'`. |
| 105 | +- `pattern:[^0-9]` -- qualunque carattere tranne un numero, come `pattern:\D`. |
| 106 | +- `pattern:[^\s]` -- qualunque carattere che non sia uno spazio, come `\S`. |
107 | 107 |
|
108 | | -The example below looks for any characters except letters, digits and spaces: |
| 108 | +L'esempio seguente cerca qualunque carattere eccetto lettere, numeri e spazi: |
109 | 109 |
|
110 | 110 | ```js run |
111 | | -alert( "alice15@gmail.com".match(/[^\d\sA-Z]/gi) ); // @ and . |
| 111 | +alert( "alice15@gmail.com".match(/[^\d\sA-Z]/gi) ); // @ e . |
112 | 112 | ``` |
113 | 113 |
|
114 | | -## Escaping in [β¦] |
| 114 | +## L'escape dentro [β¦] |
115 | 115 |
|
116 | | -Usually when we want to find exactly a special character, we need to escape it like `pattern:\.`. And if we need a backslash, then we use `pattern:\\`, and so on. |
| 116 | +In genere quando vogliamo trovare esattamente un carattere speciale, dobbiamo effettuarne l'escape: `pattern:\.`. Se abbiamo bisogno di un backslash, allora dobbiamo usare `pattern:\\`, e così via. |
117 | 117 |
|
118 | | -In square brackets we can use the vast majority of special characters without escaping: |
| 118 | +Dentro le parentesi quadre, possiamo usare la stragrande maggioranza di caratteri speciali senza la necessitΓ di effettuarne l'escape: |
119 | 119 |
|
120 | | -- Symbols `pattern:. + ( )` never need escaping. |
121 | | -- A hyphen `pattern:-` is not escaped in the beginning or the end (where it does not define a range). |
122 | | -- A caret `pattern:^` is only escaped in the beginning (where it means exclusion). |
123 | | -- The closing square bracket `pattern:]` is always escaped (if we need to look for that symbol). |
| 120 | +- I simboli `pattern:. + ( )` non necessitano mai di escaping. |
| 121 | +- Il trattino `pattern:-` non Γ¨ preceduto da caratteri di escape all'inizio o alla fine (dove non definisce un intervallo). |
| 122 | +- Un accento circonflesso `pattern:^` Γ¨ soggetto ad escape solo all'inizio (dove significa esclusione). |
| 123 | +- La parentesi quadra di chiusura `pattern:]` dev'essere sempre soggetta ad escape (se abbiamo bisogno di cercare questo simbolo). |
124 | 124 |
|
125 | | -In other words, all special characters are allowed without escaping, except when they mean something for square brackets. |
| 125 | +In altre parole, tutti i caratteri speciali sono consentiti senza necessitΓ di escape, eccetto quando significano qualcosa all'interno delle parentesi quadre. |
126 | 126 |
|
127 | | -A dot `.` inside square brackets means just a dot. The pattern `pattern:[.,]` would look for one of characters: either a dot or a comma. |
| 127 | +Un punto `.` all'interno delle parentesi quadre significa soltanto un punto. Il modello `pattern:[.,]` cercherebbe uno dei caratteri: o un punto o una virgola. |
128 | 128 |
|
129 | | -In the example below the regexp `pattern:[-().^+]` looks for one of the characters `-().^+`: |
| 129 | +Nell'esempio seguente la regexp `pattern:[-().^+]` effettua la ricerca per uno dei caratteri `-().^+`: |
130 | 130 |
|
131 | 131 | ```js run |
132 | | -// No need to escape |
| 132 | +// Non necessita di escape |
133 | 133 | let regexp = /[-().^+]/g; |
134 | 134 |
|
135 | | -alert( "1 + 2 - 3".match(regexp) ); // Matches +, - |
| 135 | +alert( "1 + 2 - 3".match(regexp) ); // Corrispondono +, - |
136 | 136 | ``` |
137 | 137 |
|
138 | | -...But if you decide to escape them "just in case", then there would be no harm: |
| 138 | +...Ma se decidete di effettuare l'escape "per ogni evenienza", il risultato non cambierebbe: |
139 | 139 |
|
140 | 140 | ```js run |
141 | | -// Escaped everything |
| 141 | +// Escape di ogni carattere |
142 | 142 | let regexp = /[\-\(\)\.\^\+]/g; |
143 | 143 |
|
144 | | -alert( "1 + 2 - 3".match(regexp) ); // also works: +, - |
| 144 | +alert( "1 + 2 - 3".match(regexp) ); // funziona ugualmente: +, - |
145 | 145 | ``` |
146 | 146 |
|
147 | | -## Ranges and flag "u" |
| 147 | +## Intervalli e flag "u" |
148 | 148 |
|
149 | | -If there are surrogate pairs in the set, flag `pattern:u` is required for them to work correctly. |
| 149 | +Se ci sono coppie surrogate nel set, il flag `pattern:u` Γ¨ necessario affinchΓ© la ricerca funzioni correttamente. |
150 | 150 |
|
151 | | -For instance, let's look for `pattern:[π³π΄]` in the string `subject:π³`: |
| 151 | +Per esempio, cerchiamo `pattern:[π³π΄]` nella stringa `subject:π³`: |
152 | 152 |
|
153 | 153 | ```js run |
154 | | -alert( 'π³'.match(/[π³π΄]/) ); // shows a strange character, like [?] |
155 | | -// (the search was performed incorrectly, half-character returned) |
| 154 | +alert( 'π³'.match(/[π³π΄]/) ); // mostra uno strano carattere, come [?] |
| 155 | +// (la ricerca Γ¨ stata eseguita in modo errato, viene restituito mezzo-carattere) |
156 | 156 | ``` |
157 | 157 |
|
158 | | -The result is incorrect, because by default regular expressions "don't know" about surrogate pairs. |
| 158 | +Il risultato non Γ¨ corretto, perchΓ© di base le espressioni regolari "non sanno nulla" riguardo le coppie surrogate. |
159 | 159 |
|
160 | | -The regular expression engine thinks that `[π³π΄]` -- are not two, but four characters: |
161 | | -1. left half of `π³` `(1)`, |
162 | | -2. right half of `π³` `(2)`, |
163 | | -3. left half of `π΄` `(3)`, |
164 | | -4. right half of `π΄` `(4)`. |
| 160 | +Il motore delle espressioni regolari pensa che `[π³π΄]` -- non sono due, ma quattro caratteri: |
| 161 | +1. metΓ alla sinistra di `π³` `(1)`, |
| 162 | +2. metΓ alla destra di `π³` `(2)`, |
| 163 | +3. metΓ alla sinistra di `π΄` `(3)`, |
| 164 | +4. metΓ alla destra di `π΄` `(4)`. |
165 | 165 |
|
166 | | -We can see their codes like this: |
| 166 | +Possiamo vedere il suo codice in questo modo: |
167 | 167 |
|
168 | 168 | ```js run |
169 | 169 | for(let i=0; i<'π³π΄'.length; i++) { |
170 | 170 | alert('π³π΄'.charCodeAt(i)); // 55349, 56499, 55349, 56500 |
171 | 171 | }; |
172 | 172 | ``` |
173 | 173 |
|
174 | | -So, the example above finds and shows the left half of `π³`. |
| 174 | +Quini, l'esempio qui sopra trova e visualizza la metΓ alla sinistra di `π³`. |
175 | 175 |
|
176 | | -If we add flag `pattern:u`, then the behavior will be correct: |
| 176 | +Se aggiungiamo il flag `pattern:u`, allora il comportamento sarΓ corretto: |
177 | 177 |
|
178 | 178 | ```js run |
179 | 179 | alert( 'π³'.match(/[π³π΄]/u) ); // π³ |
180 | 180 | ``` |
181 | 181 |
|
182 | | -The similar situation occurs when looking for a range, such as `[π³-π΄]`. |
| 182 | +Una situazione simile si verifica quando si cerca un intervallo, come `[π³-π΄]`. |
183 | 183 |
|
184 | | -If we forget to add flag `pattern:u`, there will be an error: |
| 184 | +Se dimentichiamo di aggiungere il flag `pattern:u`, ci sarΓ un errore: |
185 | 185 |
|
186 | 186 | ```js run |
187 | | -'π³'.match(/[π³-π΄]/); // Error: Invalid regular expression |
| 187 | +'π³'.match(/[π³-π΄]/); // Errore: Invalid regular expression |
188 | 188 | ``` |
189 | 189 |
|
190 | | -The reason is that without flag `pattern:u` surrogate pairs are perceived as two characters, so `[π³-π΄]` is interpreted as `[<55349><56499>-<55349><56500>]` (every surrogate pair is replaced with its codes). Now it's easy to see that the range `56499-55349` is invalid: its starting code `56499` is greater than the end `55349`. That's the formal reason for the error. |
| 190 | +La ragione Γ¨ che senza il flag `pattern:u` le coppie surrogate sono percepite come due caratteri, quindi `[π³-π΄]` Γ¨ interpretato come `[<55349><56499>-<55349><56500>]` (ogni coppia surrogata Γ¨ sostituita con i suoi codici). Ora Γ¨ facile osservare che l'intervallo `56499-55349` non Γ¨ valido: il suo codice iniziale `56499` Γ¨ maggiore di quello finale `55349`. Questa Γ¨ la ragione formale dell'errore. |
191 | 191 |
|
192 | | -With the flag `pattern:u` the pattern works correctly: |
| 192 | +Con il flag `pattern:u` il modello funziona correttamente: |
193 | 193 |
|
194 | 194 | ```js run |
195 | | -// look for characters from π³ to π΅ |
| 195 | +// cerca i caratteri da π³ a π΅ |
196 | 196 | alert( 'π΄'.match(/[π³-π΅]/u) ); // π΄ |
197 | 197 | ``` |
0 commit comments