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/12-regexp-backreferences/article.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,26 +8,26 @@ Ci si può riferire ad un gruppo nel pattern usando `pattern:\N`, in cui `N` ind
8
8
9
9
Per comprendere chiaramente perché sia utile, consideriamo l'esercitazione seguente.
10
10
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.
12
12
13
-
How to find them?
13
+
Come trovarle?
14
14
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!"`:
16
16
17
17
```js run
18
18
let str =`He said: "She's the one!".`;
19
19
20
20
let regexp =/['"](.*?)['"]/g;
21
21
22
-
//The result is not what we'd like to have
22
+
//Il risultato non è quello che vorremmo
23
23
alert( str.match(regexp) ); // "She'
24
24
```
25
25
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.
27
27
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`.
29
29
30
-
Here's the correct code:
30
+
Ecco il codice corretto:
31
31
32
32
```js run
33
33
let str =`He said: "She's the one!".`;
@@ -39,7 +39,7 @@ let regexp = /(['"])(.*?)\1/g;
39
39
alert( str.match(regexp) ); // "She's the one!"
40
40
```
41
41
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.
43
43
44
44
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
45
45
@@ -53,7 +53,7 @@ If we use `?:` in the group, then we can't reference it. Groups that are exclude
53
53
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
54
54
```
55
55
56
-
## Backreference by name: `\k<name>`
56
+
## Riferimento all'indietro per nome: `\k<name>`
57
57
58
58
If a regexp has many parentheses, it's convenient to give them names.
0 commit comments