Skip to content

Commit af7a081

Browse files
authored
Merge pull request #321 from marcellosurdi/article/regexp-backreferences
Backreferences in pattern: \N and \k<name>
2 parents 7cce37b + da3a7d7 commit af7a081

File tree

1 file changed

+22
-22
lines changed
  • 9-regular-expressions/12-regexp-backreferences

1 file changed

+22
-22
lines changed

9-regular-expressions/12-regexp-backreferences/article.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
# Backreferences in pattern: \N and \k<name>
1+
# Riferimenti all'indietro (backreference) nei pattern: \N e \k<name>
22

3-
We can use the contents of capturing groups `pattern:(...)` not only in the result or in the replacement string, but also in the pattern itself.
3+
Possiamo usare il contenuto dei gruppi di acquisizione `pattern:(...)` non soltanto nel risultato o nella stringa di sostituzione, ma anche all'interno del pattern stesso.
44

5-
## Backreference by number: \N
5+
## Riferimento all'indietro per numero: \N
66

7-
A group can be referenced in the pattern using `pattern:\N`, where `N` is the group number.
7+
Ci si può riferire ad un gruppo nel pattern usando `pattern:\N`, in cui `N` indica il numero del gruppo.
88

9-
To make clear why that's helpful, let's consider a task.
9+
Per comprendere chiaramente perché sia utile, consideriamo l'esercitazione seguente.
1010

11-
We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match.
11+
Dobbiamo trovare le stringhe tra apici: sia quelli singoli `subject:'...'` sia quelli doppi `subject:"..."`, entrambi devono dare luogo a riscontro.
1212

13-
How to find them?
13+
Come trovarli?
1414

15-
We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like in the string `subject:"She's the one!"`:
15+
Potremmo mettere i due tipi di apici all'interno di parentesi quadre: `pattern:['"](.*?)['"]`, ma in questo modo troveremmo anche le stringhe con apici misti come `match:"...'` e `match:'..."`. Questo porterebbe a risultati inesatti quando un tipo di apice compare tra due dell'altro tipo, come nella stringa `subject:"She's the one!"`:
1616

1717
```js run
1818
let str = `He said: "She's the one!".`;
1919

2020
let regexp = /['"](.*?)['"]/g;
2121

22-
// The result is not what we'd like to have
22+
// Il risultato non è quello che vorremmo
2323
alert( str.match(regexp) ); // "She'
2424
```
2525

26-
As we can see, the pattern found an opening quote `match:"`, then the text is consumed till the other quote `match:'`, that closes the match.
26+
Come possiamo osservare, il pattern ha trovato un apice di apertura `match:"`, quindi il testo fino al successivo apice `match:'` che chiude la corrispondenza.
2727

28-
To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can wrap it into a capturing group and backreference it: `pattern:(['"])(.*?)\1`.
28+
Per accertarci che il pattern trovi l'apice di chiusura uguale a quello di apertura, possiamo racchiuderlo in un gruppo di acquisizione e fare riferimento ad esso: `pattern:(['"])(.*?)\1`.
2929

30-
Here's the correct code:
30+
Ecco il codice corretto:
3131

3232
```js run
3333
let str = `He said: "She's the one!".`;
@@ -39,27 +39,27 @@ let regexp = /(['"])(.*?)\1/g;
3939
alert( str.match(regexp) ); // "She's the one!"
4040
```
4141

42-
Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group.
42+
Ora funziona! L'interprete dell'espressione regolare trova il primo apice `pattern:(['"])` e lo memorizza. Questo è il primo gruppo di acquisizione.
4343

44-
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
44+
Più avanti nel pattern `pattern:\1` significa "trova lo stesso testo del primo gruppo", nel nostro caso esattamente lo stesso apice.
4545

46-
Similar to that, `pattern:\2` would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on.
46+
Similmente `pattern:\2` indicherebbe il contenuto del secondo gruppo, `pattern:\3` quello del terzo gruppo, e così via.
4747

4848
```smart
49-
If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not memorized by the engine.
49+
Se nel gruppo usiamo `?:` non sarà possibile riferirsi ad esso. I gruppi esclusi dall'acquisizione `(?:...)` non sono memorizzati dall'interprete.
5050
```
5151

52-
```warn header="Don't mess up: in the pattern `pattern:\1`, in the replacement: `pattern:$1`"
53-
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
52+
```warn header="Non fare confusione: nel pattern `pattern:\1`, nelle sostituzioni `pattern:$1`"
53+
Nelle sostituzioni di stringa si adopera il segno di dollaro: `pattern:$1`, mentre nel contesto di un pattern il backslash `pattern:\1`.
5454
```
5555
56-
## Backreference by name: `\k<name>`
56+
## Riferimento all'indietro per nome: `\k<name>`
5757
58-
If a regexp has many parentheses, it's convenient to give them names.
58+
Se un'espressione regolare ha tante parentesi, è opportuno dare loro dei nomi.
5959
60-
To reference a named group we can use `pattern:\k<name>`.
60+
Per riferirsi ad un gruppo nominato si usa `pattern:\k<name>`.
6161
62-
In the example below the group with quotes is named `pattern:?<quote>`, so the backreference is `pattern:\k<quote>`:
62+
Nell'esempio sotto il gruppo con gli apici è nominato `pattern:?<quote>`, pertanto il riferimento è `pattern:\k<quote>`:
6363
6464
```js run
6565
let str = `He said: "She's the one!".`;

0 commit comments

Comments
 (0)