Skip to content

Commit 8df25e8

Browse files
committed
Update 9-regular-expressions\14-regexp-lookahead-lookbehind
1 parent 1057dbe commit 8df25e8

File tree

1 file changed

+20
-20
lines changed
  • 9-regular-expressions/14-regexp-lookahead-lookbehind

1 file changed

+20
-20
lines changed

9-regular-expressions/14-regexp-lookahead-lookbehind/article.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,62 @@ Sono possibili test più complessi, ad esempio `pattern:X(?=Y)(?=Z)` significa:
2929
3. Verifica se `pattern:Z` sia anch'esso dopo `pattern:X` (non proseguire in caso contrario).
3030
4. Se entrambi i test trovano riscontro considera `pattern:X` una corrispondenza, diversamente continua la ricerca.
3131

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

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

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)`:
3737

3838
```js run
3939
let str = "1 turkey costs 30€";
4040

4141
alert( str.match(/\d+(?=\s)(?=.*30)/) ); // 1
4242
```
4343

44-
In our string that exactly matches the number `1`.
44+
Nella nostra stringa trova esatta corrispondenza nel numero `1`.
4545

46-
## Negative lookahead
46+
## Lookahead negativo
4747

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:€`.
4949

50-
For that, a negative lookahead can be applied.
50+
A questo scopo può essere applicato un lookahead negativo.
5151

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`".
5353

5454
```js run
5555
let str = "2 turkeys cost 60€";
5656

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)
5858
```
5959

6060
## Lookbehind
6161

62-
Lookahead allows to add a condition for "what follows".
62+
Lookahead permette di porre una condizione per "quello che segue".
6363

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

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

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:$`:
7171

7272
```js run
7373
let str = "1 turkey costs $30";
7474

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)
7777
```
7878

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+`:
8080

8181
```js run
8282
let str = "2 turkeys cost $60";
8383

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)
8585
```
8686

87-
## Capturing groups
87+
## Gruppi di acquisizione
8888

8989
Generally, the contents inside lookaround parentheses does not become a part of the result.
9090

0 commit comments

Comments
 (0)