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/17-regexp-methods/article.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,12 +150,12 @@ Le second argument correspond au texte de remplacement. Il est possible d'utilis
150
150
151
151
| Symbols | Action in the replacement string |
152
152
|--------|--------|
153
-
|`$&`|inserts the whole match|
154
-
|<code>$`</code>|inserts a part of the string before the match|
155
-
|`$'`|inserts a part of the string after the match|
156
-
|`$n`|if`n`is a 1-2 digit number, inserts the contents of n-th capturing group, for details see [](info:regexp-groups)|
157
-
|`$<name>`|inserts the contents of the parentheses with the given `name`, for details see [](info:regexp-groups)|
158
-
|`$$`|inserts character`$` |
153
+
|`$&`|insère la chaine de caractère en correspondance|
154
+
|<code>$`</code>|Insère la partie de la chaîne de caractère qui précède la sous-chaîne en correspondance|
155
+
|`$'`|insère la partie de la chaîne de caractère qui suit la sous-chaîne en correspondance|
156
+
|`$n`|si`n`est un nombre à 1 ou 2 chiffres, insère la n-ième chaîne de sous-correspondance entre parenthèses, pour plus de détails voir [](info:regexp-groups)|
157
+
|`$<name>`|insère la chaîne de caractère du `name` correspondant à celui entre parenthèse, pour plus de détails voir [](info:regexp-groups)|
If the regexp has flag `pattern:g`, then`regexp.test`looks from `regexp.lastIndex`property and updates this property, just like`regexp.exec`.
320
+
Si la regexp à le marqueur `pattern:g`, alors`regexp.test`verifiera la propriété `regexp.lastIndex`et mettra à jours cette propriété, tout comme`regexp.exec`.
321
321
322
-
So we can use it to search from a given position:
322
+
On peut donc l'utiliser pour effectuer une recherche à partir d'un indice donnée:
323
323
324
324
```js run
325
325
let regexp =/love/gi;
326
326
327
327
let str ="I love JavaScript";
328
328
329
-
//start the search from position 10:
329
+
//commence la recherche à partir de l'indice 10:
330
330
regexp.lastIndex=10;
331
-
alert( regexp.test(str) ); // false (no match)
331
+
alert( regexp.test(str) ); // false (pas de correspondance)
332
332
```
333
333
334
-
````warn header="Same global regexp tested repeatedly on different sources may fail"
335
-
If we apply the same global regexp to different inputs, it may lead to wrong result, because `regexp.test`call advances `regexp.lastIndex` property, so the search in another string may start from non-zero position.
334
+
````warn header="Une même expression rationnelle testée de manière répétée sur différentes sources peut échouer"
335
+
Appliquer la même expression rationnelle globale sur différentes entrées peut conduire à de mauvais résultats, car l'appel à `regexp.test`modifie la propriété `regexp.lastIndex`, par conséquent la recherche sur une autre chaîne de caractères risque d'être lancer à partir d'un autre indice que `0`.
336
336
337
-
For instance, here we call`regexp.test`twice on the same text, and the second time fails:
337
+
Par exemple, nous appelons ici`regexp.test`à deux reprises sur la même chaîne de texte, and le second appel échoue:
338
338
339
339
```js run
340
-
let regexp =/javascript/g; // (regexp just created: regexp.lastIndex=0)
340
+
let regexp =/javascript/g; // (création d'une nouvelle regexp: regexp.lastIndex=0)
That's exactly because `regexp.lastIndex`is non-zero in the second test.
346
+
C'est exactement parce que `regexp.lastIndex`n'est pas `0` lors du second test.
347
347
348
-
To work around that, we can set`regexp.lastIndex=0`before each search. Or instead of calling methods on regexp, use string methods `str.match/search/...`, they don't use`lastIndex`.
348
+
Afin de contourner cela, nous pouvons réinitialiser`regexp.lastIndex=0`avant chaque recherche. Ou, au lieu d'appeler la méthode sur une regexp, nous pouvons utiliser les méthodes de l'objet String `str.match/search/...`, qui n'utilisent pas`lastIndex`.
0 commit comments