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
Le mode multiligne est activé avec le marqueur`pattern:m`.
3
+
The multiline mode is enabled by the flag`pattern:m`.
4
4
5
-
Il affecte seulement le fonctionnement des ancres `pattern:^`et`pattern:$`.
5
+
It only affects the behavior of `pattern:^`and`pattern:$`.
6
6
7
-
Dans le mode multiligne, elles ne vérifient pas seulement le début et la fin d'une chaîne de caractères, mais également le début et la fin d'une ligne.
7
+
In the multiline mode they match not only at the beginning and the end of the string, but also at start/end of line.
8
8
9
-
## Recherche au début de ligne ^
9
+
## Searching at line start ^
10
10
11
-
Dans l'exemple ci-dessous, le texte comporte plusieurs lignes. Le paterne`pattern:/^\d/gm`prend un chiffre au début de chaque ligne:
11
+
In the example below the text has multiple lines. The pattern`pattern:/^\d/gm`takes a digit from the beginning of each line:
Cela est dû au fait que, par défaut, l'accent circonflexe `pattern:^`ne vérifie que le début d'un texte, et dans le mode multiligne -- le début de n'importe quelle ligne.
35
+
That's because by default a caret `pattern:^`only matches at the beginning of the text, and in the multiline mode -- at the start of any line.
36
36
37
37
```smart
38
-
"Début de ligne" désigne formellement "immédiatement après un saut à la ligne" : le test `pattern:^` en mode multiligne vérifie à toutes les positions suivant un caractère de retour à la ligne `\n`, en plus du début du texte.
38
+
"Start of a line" formally means "immediately after a line break": the test `pattern:^` in multiline mode matches at all positions preceeded by a newline character `\n`.
39
+
40
+
And at the text start.
39
41
```
40
42
41
-
## Recherche en fin de ligne $
43
+
## Searching at line end $
42
44
43
-
Le signe dollar `pattern:$`se comporte similairement.
45
+
The dollar sign `pattern:$`behaves similarly.
44
46
45
-
L'expression régulière `pattern:\d$`prend le premier chiffre à la fin de chaque ligne.
47
+
The regular expression `pattern:\d$`finds the last digit in every line
46
48
47
49
```js run
48
50
let str =`Winnie: 1
@@ -52,19 +54,21 @@ Eeyore: 3`;
52
54
alert( str.match(/\d$/gm) ); // 1,2,3
53
55
```
54
56
55
-
Sans le marqueur `pattern:m`, le signe dollar `pattern:$`vérifierait uniquement la fin de tout le texte, donc uniquement le tout dernier chiffre serait trouvé.
57
+
Without the flag `m`, the dollar `pattern:$`would only match the end of the whole text, so only the very last digit would be found.
56
58
57
59
```smart
58
-
"Fin de ligne" désigne formellement "immédiatement avant un saut à la ligne" : le test `pattern:$` en mode multiligne vérifie à toutes les positions précédant un caractère de retour à la ligne `\n`, en plus de la fin du texte.
60
+
"End of a line" formally means "immediately before a line break": the test `pattern:$` in multiline mode matches at all positions succeeded by a newline character `\n`.
61
+
62
+
And at the text end.
59
63
```
60
64
61
-
## Recherche de \n au lieu de ^ $
65
+
## Searching for \n instead of ^ $
62
66
63
-
Pour chercher un retour à la ligne, nous pouvons non seulement utiliser les ancres `pattern:^`et`pattern:$`, mais également le caractère de retour à la ligne`\n`.
67
+
To find a newline, we can use not only anchors `pattern:^`and`pattern:$`, but also the newline character`\n`.
64
68
65
-
Quelle est la différence ? Regardons un exemple.
69
+
What's the difference? Let's see an example.
66
70
67
-
Ici, nous cherchons `pattern:\d\n`au lieu de`pattern:\d$`:
71
+
Here we search for `pattern:\d\n`instead of`pattern:\d$`:
68
72
69
73
```js run
70
74
let str =`Winnie: 1
@@ -74,10 +78,10 @@ Eeyore: 3`;
74
78
alert( str.match(/\d\n/gm) ); // 1\n,2\n
75
79
```
76
80
77
-
Comme nous pouvons le voir, il y a 2 correspondances au lieu de 3.
81
+
As we can see, there are 2 matches instead of 3.
78
82
79
-
C'est parce qu'il n'y a pas de retour à la ligne après `subject:3` (il y a la fin du texte cependant, donc ça correspond avec`pattern:$`).
83
+
That's because there's no newline after `subject:3` (there's text end though, so it matches`pattern:$`).
80
84
81
-
Une autre différence : maintenant, chaque correspondance inclue un caractère de retour à la ligne `match:\n`. Contrairement aux ancres`pattern:^``pattern:$`, qui ne testent qu'une condition (début/fin d'une ligne), `\n`est un caractère, donc il devient une partie du résultat.
85
+
Another difference: now every match includes a newline character `match:\n`. Unlike the anchors`pattern:^``pattern:$`, that only test the condition (start/end of a line), `\n`is a character, so it becomes a part of the result.
82
86
83
-
Ainsi, un`\n`dans le paterne est utilisé quand nous avons besoin dudit caractère dans le résultat, tandis que les ancres sont utilisées pour chercher quelque chose au début/à la fin d'une ligne.
87
+
So, a`\n`in the pattern is used when we need newline characters in the result, while anchors are used to find something at the beginning/end of a line.
Copy file name to clipboardExpand all lines: 9-regular-expressions/12-regexp-backreferences/article.md
+22-22Lines changed: 22 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,33 @@
1
-
# Backreferences in pattern: \N and \k<name>
1
+
# Rétro référence dans le paterne : \N et \k<name>
2
2
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
+
Nous pouvons utiliser le contenu des groupes de capture `pattern:(...)`non seulement dans le résultat ou dans la chaîne de caractères de remplacement, mais également dans le paterne en lui-même.
4
4
5
-
## Backreference by number: \N
5
+
## Rétro référence par un nombre : \N
6
6
7
-
A group can be referenced in the pattern using`pattern:\N`, where`N`is the group number.
7
+
Un groupe peut être référencé dans le paterne par`pattern:\N`, où`N`est le numéro du groupe.
8
8
9
-
To make clear why that's helpful, let's consider a task.
9
+
Pour rendre son utilité claire, considérons la tâche ci-dessous.
10
10
11
-
We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match.
11
+
Nous devons trouver des chaînes citées : soit par des apostrophes `subject:'...'`, soit par des guillemets `subject:"..."` -- les deux variantes devraient correspondre.
12
12
13
-
How to find them?
13
+
Comment les trouver ?
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
+
Nous pouvons mettre les deux types entre crochets : `pattern:['"](.*?)['"]`, mais ce paterne pourrait correspondre avec des mélanges comme`match:"...'`ou`match:'..."`. Cela mènerait à des correspondances incorrectes lorsqu'une citation apparaît dans une autre, comme dans le texte`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
+
//Le résultat n'est pas celui que nous aimerions avoir
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
+
Comme nous pouvons le voir, le paterne trouve des guillemets ouvrant`match:"`, puis le texte est récupéré jusqu'au `match:'`, ce qui termine la correspondance.
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
+
Pour faire en sorte que le paterne vérifie que le caractère terminant la citation est précisément le même que celui qui l'ouvre, nous pouvons l'envelopper dans un groupe de capture et le rétro référencier : `pattern:(['"])(.*?)\1`.
29
29
30
-
Here's the correct code:
30
+
Voilà le code correct :
31
31
32
32
```js run
33
33
let str =`He said: "She's the one!".`;
@@ -39,27 +39,27 @@ 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
+
Maintenant, ça fonctionne ! Le moteur trouve le premier caractère de citation`pattern:(['"])`et mémorise son contenu. C'est le premier groupe de capture.
43
43
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
+
Plus loin dans le paterne, `pattern:\1`signifie "cherche le même texte que dans le premier groupe de capture", le même caractère de citation dans notre cas.
45
45
46
-
Similar to that, `pattern:\2`would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on.
46
+
Similairement, `pattern:\2`voudrait référencier le 2nd groupe, `pattern:\3` - le 3e groupe, et ainsi de suite.
47
47
48
48
```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
+
Si nous utilisons `?:` dans le groupe, alors nous ne pouvons pas le référencer. Les groupes exclus de la capture `(?:...)` ne sont pas mémorisés par le moteur.
50
50
```
51
51
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="Ne mélangez pas : dans le paterne,`pattern:\1`, dans le replacement: `pattern:$1`"
53
+
Dans la chaîne de remplacement, on utilise un signe dollar : `pattern:$1`, alors que dans un paterne - un antislash`pattern:\1`.
54
54
```
55
55
56
-
## Backreference by name: `\k<name>`
56
+
## Rétro référence par le nom: `\k<name>`
57
57
58
-
If a regexp has many parentheses, it's convenient to give them names.
58
+
Si une expression régulière a beaucoup de groupes, il est pratique de leur attribuer un nom.
59
59
60
-
To reference a named group we can use `pattern:\k<имя>`.
60
+
Pour référencer un groupe nommé, on peut utiliser `pattern:\k<имя>`.
61
61
62
-
In the example below the group with quotes is named `pattern:?<quote>`, so the backreference is `pattern:\k<quote>`:
62
+
Dans l'exemple ci-dessous, le groupe du caractère de citation s'appelle `pattern:?<quote>`, donc la rétro référence est `pattern:\k<quote>`:
0 commit comments