Skip to content

Commit 34dd784

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

File tree

1 file changed

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

1 file changed

+16
-16
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,22 @@ alert( str.match(/(?<!\$)\b\d+/g) ); // 2 (il risultato non include il prezzo)
8686

8787
## Gruppi di acquisizione
8888

89-
Generally, the contents inside lookaround parentheses does not become a part of the result.
89+
Generalmente il contenuto dentro le parentesi di lookaround non diventa parte del risultato.
9090

91-
E.g. in the pattern `pattern:\d+(?=€)`, the `pattern:€` sign doesn't get captured as a part of the match. That's natural: we look for a number `pattern:\d+`, while `pattern:(?=€)` is just a test that it should be followed by `subject:€`.
91+
Nel pattern `pattern:\d+(?=€)`, ad esempio, il segno `pattern:€` non viene acquisito nella corrispondenza. È del tutto normale: stiamo cercando il numero `pattern:\d+`, mentre `pattern:(?=€)` è solo un test che indica che il numero dovrebbe essere seguito da `subject:€`.
9292

93-
But in some situations we might want to capture the lookaround expression as well, or a part of it. That's possible. Just wrap that part into additional parentheses.
93+
In alcune situazioni, tuttavia, potremmo voler catturare anche l'espressione del lookaround, o una parte di essa. Questo è possibile: è sufficiente racchiudere la parte desiderata all'interno di parentesi aggiuntive.
9494

95-
In the example below the currency sign `pattern:(€|kr)` is captured, along with the amount:
95+
Nell'esempio sotto, il segno di valuta `pattern:(€|kr)` viene acquisito insieme all'importo:
9696

9797
```js run
9898
let str = "1 turkey costs 30€";
99-
let regexp = /\d+(?=(€|kr))/; // extra parentheses around €|kr
99+
let regexp = /\d+(?=(€|kr))/; // parentesi addizionali intorno €|kr
100100

101101
alert( str.match(regexp) ); // 30, €
102102
```
103103

104-
And here's the same for lookbehind:
104+
Stesso discorso per il lookbehind:
105105

106106
```js run
107107
let str = "1 turkey costs $30";
@@ -112,19 +112,19 @@ alert( str.match(regexp) ); // 30, $
112112

113113
## Riepilogo
114114

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.
115+
Il lookahead e il lookbehind (comunemente denominati con il termine "lookaround") sono utili quando vogliamo trovare qualcosa in base a ciò viene prima o dopo di esso.
116116

117-
For simple regexps we can do the similar thing manually. That is: match everything, in any context, and then filter by context in the loop.
117+
Nel caso di espressioni regolari semplici potremmo ottenere lo stesso risultato manualmente. In altre parole: troviamo ogni riscontro, e quindi filtriamo i risultati in base alla posizione nel ciclo iterativo.
118118

119-
Remember, `str.match` (without flag `pattern:g`) and `str.matchAll` (always) return matches as arrays with `index` property, so we know where exactly in the text it is, and can check the context.
119+
Ricordiamoci che `str.match` (senza il flag `pattern:g`) e `str.matchAll` (sempre) restituiscono i risultati in un array con la proprietà `index`, conosciamo pertanto l'esatta posizione della corrispondenza e possiamo stabilirne il contesto.
120120

121-
But generally lookaround is more convenient.
121+
Generalmente, però, il lookaround è più efficiente.
122122

123-
Lookaround types:
123+
Tipi di lookaround:
124124

125-
| Pattern | type | matches |
125+
| Pattern | Tipo | Riscontri |
126126
|--------------------|------------------|---------|
127-
| `X(?=Y)` | Positive lookahead | `pattern:X` if followed by `pattern:Y` |
128-
| `X(?!Y)` | Negative lookahead | `pattern:X` if not followed by `pattern:Y` |
129-
| `(?<=Y)X` | Positive lookbehind | `pattern:X` if after `pattern:Y` |
130-
| `(?<!Y)X` | Negative lookbehind | `pattern:X` if not after `pattern:Y` |
127+
| `X(?=Y)` | Lookahead positivo | `pattern:X` se seguito da `pattern:Y` |
128+
| `X(?!Y)` | Lookahead negativo | `pattern:X` se seguito da `pattern:Y` |
129+
| `(?<=Y)X` | Lookbehind positivo | `pattern:X` se dopo `pattern:Y` |
130+
| `(?<!Y)X` | Lookbehind negativo | `pattern:X` se dopo `pattern:Y` |

0 commit comments

Comments
 (0)