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
Copy file name to clipboardExpand all lines: 9-regular-expressions/14-regexp-lookahead-lookbehind/article.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,62 +29,62 @@ Sono possibili test più complessi, ad esempio `pattern:X(?=Y)(?=Z)` significa:
29
29
3. Verifica se `pattern:Z` sia anch'esso dopo `pattern:X` (non proseguire in caso contrario).
30
30
4. Se entrambi i test trovano riscontro considera `pattern:X` una corrispondenza, diversamente continua la ricerca.
31
31
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.
32
+
In altre parole, questo pattern significa che stiamo cercando `pattern:X`seguito sia da `pattern:Y`sia da `pattern:Z`.
33
33
34
-
That's only possible if patterns `pattern:Y`and`pattern:Z`aren't mutually exclusive.
34
+
Il che è possibile solo se i pattern `pattern:Y`e`pattern:Z`non si escludono a vicenda.
35
35
36
-
For example, `pattern:\d+(?=\s)(?=.*30)`looks for `pattern:\d+`that is followed by a space `pattern:(?=\s)`, and there's`30`somewhere after it`pattern:(?=.*30)`:
36
+
Per esempio, `pattern:\d+(?=\s)(?=.*30)`cerca `pattern:\d+`seguito da uno spazio `pattern:(?=\s)`, e poi c'è`30`da qualche parte dopo di esso`pattern:(?=.*30)`:
37
37
38
38
```js run
39
39
let str ="1 turkey costs 30€";
40
40
41
41
alert( str.match(/\d+(?=\s)(?=.*30)/) ); // 1
42
42
```
43
43
44
-
In our string that exactly matches the number`1`.
44
+
Nella nostra stringa trova esatta corrispondenza nel numero`1`.
45
45
46
-
## Negative lookahead
46
+
## Lookahead negativo
47
47
48
-
Let's say that we want a quantity instead, not a price from the same string. That's a number`pattern:\d+`, NOT followed by`subject:€`.
48
+
Supponiamo invece di volere nella stessa stringa solo la quantità, non il prezzo. Quindi il numero`pattern:\d+`, NON seguito da`subject:€`.
49
49
50
-
For that, a negative lookahead can be applied.
50
+
A questo scopo può essere applicato un lookahead negativo.
51
51
52
-
The syntax is: `pattern:X(?!Y)`, it means "search`pattern:X`, but only if not followed by`pattern:Y`".
52
+
La sintassi è: `pattern:X(?!Y)`, significa "cerca`pattern:X`, ma solo se non seguito da`pattern:Y`".
53
53
54
54
```js run
55
55
let str ="2 turkeys cost 60€";
56
56
57
-
alert( str.match(/\d+\b(?!€)/g) ); // 2 (the price is not matched)
57
+
alert( str.match(/\d+\b(?!€)/g) ); // 2 (il prezzo non costituisce corrispondenza)
58
58
```
59
59
60
60
## Lookbehind
61
61
62
-
Lookahead allows to add a condition for "what follows".
62
+
Lookahead permette di porre una condizione per "quello che segue".
63
63
64
-
Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before it.
64
+
Lookbehind è simile, ma cerca quello che precede. Consente quindi di trovare una corrispondenza per un pattern solo se c'è qualcosa prima di esso.
65
65
66
-
The syntax is:
67
-
-Positive lookbehind: `pattern:(?<=Y)X`, matches`pattern:X`, but only if there's `pattern:Y`before it.
68
-
-Negative lookbehind: `pattern:(?<!Y)X`, matches`pattern:X`, but only if there's no`pattern:Y`before it.
66
+
La sintassi è:
67
+
-Lookbehind positivo: `pattern:(?<=Y)X`, trova`pattern:X`, ma solo se c'è `pattern:Y`prima di esso.
68
+
-Lookbehind negativo: `pattern:(?<!Y)X`, trova`pattern:X`, ma solo se non c'è alcun`pattern:Y`prima di esso.
69
69
70
-
For example, let's change the price to US dollars. The dollar sign is usually before the number, so to look for `$30`we'll use `pattern:(?<=\$)\d+`-- an amount preceded by`subject:$`:
70
+
Cambiamo, ad esempio, il prezzo in dollari USA. Il segno del dollaro è posto di solito prima del numero, per cercare pertanto `$30`useremo `pattern:(?<=\$)\d+`un importo preceduto da`subject:$`:
71
71
72
72
```js run
73
73
let str ="1 turkey costs $30";
74
74
75
-
//the dollar sign is escaped \$
76
-
alert( str.match(/(?<=\$)\d+/) ); // 30 (skipped the sole number)
75
+
//facciamo l'escape al segno del dollaro \$
76
+
alert( str.match(/(?<=\$)\d+/) ); // 30 (salta il numero senza segno di valuta)
77
77
```
78
78
79
-
And, if we need the quantity -- a number, not preceded by`subject:$`, then we can use a negative lookbehind `pattern:(?<!\$)\d+`:
79
+
Se abbiamo bisogno della quantità, il numero, non preceduto da`subject:$`, allora possiamo usare il lookbehind negativo`pattern:(?<!\$)\d+`:
80
80
81
81
```js run
82
82
let str ="2 turkeys cost $60";
83
83
84
-
alert( str.match(/(?<!\$)\b\d+/g) ); // 2 (the price is not matched)
84
+
alert( str.match(/(?<!\$)\b\d+/g) ); // 2 (il risultato non include il prezzo)
85
85
```
86
86
87
-
## Capturing groups
87
+
## Gruppi di acquisizione
88
88
89
89
Generally, the contents inside lookaround parentheses does not become a part of the result.
0 commit comments