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
+18-17Lines changed: 18 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ La méthode `str.match(regexp)` trouve les correspondances de l'expression regul
10
10
11
11
Elle dispose de 3 modes :
12
12
13
-
1. si la regexpn'à pas de marqueur pattern:g, alors seul la première correspondance est retournée sous la forme d'un tableau avec le groupe capturé et ses propriétés index (position de la correspondance), et input (chaîne d'entrée équivalent à str):
13
+
1. si la regexp n'à pas de marqueur `pattern:g`, alors seul la première correspondance est retournée sous la forme d'un tableau avec le groupe capturé et ses propriétés : index (position de la correspondance), et input (chaîne d'entrée équivalent à str):
14
14
15
15
```js run
16
16
let str ="I love JavaScript";
@@ -60,27 +60,28 @@ Elle dispose de 3 modes :
60
60
61
61
[recent browser="new"]
62
62
63
-
The method `str.matchAll(regexp)` is a "newer, improved" variant of `str.match`.
63
+
La méthode `str.matchAll(regexp)` est une variante "améliorée" de `str.match`.
64
64
65
+
Elle est principalement utilisée pour rechercher toutes les correspondances au sein de tous les groupes.
65
66
It's used mainly to search for all matches with all groups.
66
67
67
-
There are 3 differences from`match`:
68
+
Il y a 3 différences avec`match`:
68
69
69
-
1.It returns an iterable object with matches instead of an array. We can make a regular array from it using`Array.from`.
70
-
2.Every match is returned as an array with capturing groups (the same format as`str.match`without flag`pattern:g`).
71
-
3.If there are no results, it returns not `null`, but an empty iterable object.
70
+
1.Elle retourne un objet iterable avec les correspondances au lieu d'un tableau. Nous pouvons le transformer en un tableau en utilisant la méthode `Array.from`.
71
+
2. Toutes les correspondances sont retournées dans un tableau incluant les groupes capturants (sous le même format que `str.match` sans le marqueur `pattern:g`).
72
+
3. Si aucun résultat, `null` n'est pas retourné, mais un objet itérable vide.
72
73
73
-
Usage example:
74
+
Exemple d'utilisation:
74
75
75
76
```js run
76
77
let str = '<h1>Hello, world!</h1>';
77
78
let regexp = /<(.*?)>/g;
78
79
79
80
let matchAll = str.matchAll(regexp);
80
81
81
-
alert(matchAll); // [object RegExp String Iterator], not array, but an iterable
82
+
alert(matchAll); // [object RegExp String Iterator], pas un tableau, mais un itérateur
82
83
83
-
matchAll = Array.from(matchAll); // array now
84
+
matchAll = Array.from(matchAll); // maintenant un tableau
If we use`for..of`to loop over `matchAll` matches, then we don't need `Array.from` any more.
93
+
Si nous utilisons `for..of` pour boucler sur les résultats de `matchAll`, alors `Array.from` n'est plus utile.
93
94
94
95
## str.split(regexp|substr, limit)
95
96
96
-
Splits the string using the regexp (or a substring) as a delimiter.
97
+
Diviser la chaîne de caractères en utilisant la regexp (ou une sous-chaîne de caractères) comme délimiteur.
97
98
98
-
We can use `split` with strings, like this:
99
+
Nous pouvons utiliser`split`avec un chaîne de caractères comme ceci :
99
100
100
101
```js run
101
102
alert('12-34-56'.split('-')) // array of [12, 34, 56]
102
103
```
103
104
104
-
But we can split by a regular expression, the same way:
105
+
Mais nous pouvons aussi diviser une chaîne de texte en utilisant une expression regulière:
105
106
106
107
```js run
107
108
alert('12, 34, 56'.split(/,\s*/)) // array of [12, 34, 56]
108
109
```
109
110
110
111
## str.search(regexp)
111
112
112
-
The method `str.search(regexp)` returns the position of the first match or `-1` if none found:
113
+
La méthode`str.search(regexp)`renvoie la position du premier motif correspondant, ou`-1`si aucune correspondance n'est trouvée:
113
114
114
115
```js run
115
116
let str = "A drop of ink may make a million think";
116
117
117
-
alert( str.search( /ink/i ) ); // 10 (first match position)
118
+
alert( str.search( /ink/i ) ); // 10 (position du premier motif correspondant)
118
119
```
119
120
120
-
**The important limitation: `search` only finds the first match.**
121
+
**Limitation importante: `search` renvoie uniquement la première correspondance.**
121
122
122
-
If we need positions of further matches, we should use other means, such as finding them all with `str.matchAll(regexp)`.
123
+
Si nous avons besoin de la position ou de plus de correspondances, nous devrions utiliser d'autres méthodes, comme les trouver tous avec `str.matchAll(regexp)`.
0 commit comments