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
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,22 +86,22 @@ alert( str.match(/(?<!\$)\b\d+/g) ); // 2 (il risultato non include il prezzo)
86
86
87
87
## Gruppi di acquisizione
88
88
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.
90
90
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:€`.
92
92
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.
94
94
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:
96
96
97
97
```js run
98
98
let str ="1 turkey costs 30€";
99
-
let regexp =/\d+(?=(€|kr))/; //extra parentheses around €|kr
99
+
let regexp =/\d+(?=(€|kr))/; //parentesi addizionali intorno €|kr
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.
116
116
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.
118
118
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.
120
120
121
-
But generally lookaround is more convenient.
121
+
Generalmente, però, il lookaround è più efficiente.
0 commit comments