|
2 | 2 |
|
3 | 3 | Talvolta abbiamo bisogno di trovare soltanto quei riscontri per un pattern che sono seguiti o preceduti da un altro pattern. |
4 | 4 |
|
5 | | -There's a special syntax for that, called "lookahead" and "lookbehind", together referred to as "lookaround". |
| 5 | +Esiste a questo scopo una sintassi speciale denominata "lookahead" e "lookbehind", indicata complessivamente con il termine "lookaround". |
6 | 6 |
|
7 | | -For the start, let's find the price from the string like `subject:1 turkey costs 30€`. That is: a number, followed by `subject:€` sign. |
| 7 | +Per cominciare troviamo il prezzo in una stringa come `subject:1 turkey costs 30€`. In parole semplici: un numero seguito dal simbolo di valuta `subject:€`. |
8 | 8 |
|
9 | 9 | ## Lookahead |
10 | 10 |
|
11 | | -The syntax is: `pattern:X(?=Y)`, it means "look for `pattern:X`, but match only if followed by `pattern:Y`". There may be any pattern instead of `pattern:X` and `pattern:Y`. |
| 11 | +La sintassi è: `pattern:X(?=Y)`, che significa "cerca `pattern:X`, ma trova la corrispondenza solo se seguita da `pattern:Y`". Possiamo sostituire `pattern:X` e `pattern:Y` con un pattern qualsiasi. |
12 | 12 |
|
13 | | -For an integer number followed by `subject:€`, the regexp will be `pattern:\d+(?=€)`: |
| 13 | +Per un numero intero seguito da `subject:€`, la regexp sarà `pattern:\d+(?=€)`: |
14 | 14 |
|
15 | 15 | ```js run |
16 | 16 | let str = "1 turkey costs 30€"; |
17 | 17 |
|
18 | | -alert( str.match(/\d+(?=€)/) ); // 30, the number 1 is ignored, as it's not followed by € |
| 18 | +alert( str.match(/\d+(?=€)/) ); // 30, viene ignorato il numero 1 in quanto non seguito da € |
19 | 19 | ``` |
20 | 20 |
|
21 | | -Please note: the lookahead is merely a test, the contents of the parentheses `pattern:(?=...)` is not included in the result `match:30`. |
| 21 | +Si noti che la parte lookahead è solo un test e pertanto il contenuto tra parentesi `pattern:(?=...)` non è incluso nel risultato `match:30`. |
22 | 22 |
|
23 | | -When we look for `pattern:X(?=Y)`, the regular expression engine finds `pattern:X` and then checks if there's `pattern:Y` immediately after it. If it's not so, then the potential match is skipped, and the search continues. |
| 23 | +Quando cerchiamo `pattern:X(?=Y)` l'interprete dell'espressione regolare trova `pattern:X` e successivamente verifica anche la presenza di `pattern:Y` subito dopo di esso. In caso contrario la corrispondenza potenziale viene scartata e la ricerca prosegue. |
24 | 24 |
|
25 | | -More complex tests are possible, e.g. `pattern:X(?=Y)(?=Z)` means: |
| 25 | +Sono possibili test più complessi, ad esempio `pattern:X(?=Y)(?=Z)` significa: |
26 | 26 |
|
27 | | -1. Find `pattern:X`. |
28 | | -2. Check if `pattern:Y` is immediately after `pattern:X` (skip if isn't). |
29 | | -3. Check if `pattern:Z` is also immediately after `pattern:X` (skip if isn't). |
30 | | -4. If both tests passed, then the `pattern:X` is a match, otherwise continue searching. |
| 27 | +1. Trova `pattern:X`. |
| 28 | +2. Verifica se `pattern:Y` sia subito dopo `pattern:X` (non proseguire in caso contrario). |
| 29 | +3. Verifica se `pattern:Z` sia anch'esso dopo `pattern:X` (non proseguire in caso contrario). |
| 30 | +4. Se entrambi i test trovano riscontro considera `pattern:X` una corrispondenza, diversamente continua la ricerca. |
31 | 31 |
|
32 | 32 | In other words, such pattern means that we're looking for `pattern:X` followed by `pattern:Y` and `pattern:Z` at the same time. |
33 | 33 |
|
@@ -110,7 +110,7 @@ let regexp = /(?<=(\$|£))\d+/; |
110 | 110 | alert( str.match(regexp) ); // 30, $ |
111 | 111 | ``` |
112 | 112 |
|
113 | | -## Summary |
| 113 | +## Riepilogo |
114 | 114 |
|
115 | 115 | Lookahead and lookbehind (commonly referred to as "lookaround") are useful when we'd like to match something depending on the context before/after it. |
116 | 116 |
|
|
0 commit comments