Skip to content

Commit f0b2493

Browse files
committed
Update 9-regular-expressions\12-regexp-backreferences
1 parent baa30e1 commit f0b2493

File tree

1 file changed

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

1 file changed

+9
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ Ci si può riferire ad un gruppo nel pattern usando `pattern:\N`, in cui `N` ind
88

99
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 trovarle?
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,7 +39,7 @@ 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! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group.
4343

4444
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
4545

@@ -53,7 +53,7 @@ If we use `?:` in the group, then we can't reference it. Groups that are exclude
5353
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
5454
```
5555
56-
## Backreference by name: `\k<name>`
56+
## Riferimento all'indietro per nome: `\k<name>`
5757
5858
If a regexp has many parentheses, it's convenient to give them names.
5959

0 commit comments

Comments
 (0)