◐ Shell
clean mode source ↗

Capturing groups by XyoloJR · Pull Request #358 · javascript-tutorial/fr.javascript.info

@@ -1,44 +1,44 @@ A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in the previous task. Une regexp pour un nombre : `pattern:-?\d+(\.\d+)?`. Nous l'avons vu dans l'exercice précédent.
An operator is `pattern:[-+*/]`. The hyphen `pattern:-` goes first in the square brackets, because in the middle it would mean a character range, while we just want a character `-`. Pour l'opérateur `pattern:[-+*/]`. Le tiret `pattern:-` est en premier, car il pourrait signifier un intervalle de caractère, alors que nous souhaitons juste le caractère `-`.
The slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later. Le slash `/` doit être échappé en javascript dans une regexp `pattern:/.../`, et nous le ferons plus tard.
We need a number, an operator, and then another number. And optional spaces between them. Nous cherchons un nombre, un opérateur puis un autre nombre. Et d'éventuels espaces entre eux.
The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. Cela done l'expression régulière : `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
It has 3 parts, with `pattern:\s*` between them: 1. `pattern:-?\d+(\.\d+)?` - the first number, 1. `pattern:[-+*/]` - the operator, 1. `pattern:-?\d+(\.\d+)?` - the second number. Il y a trois parties, avec `pattern:\s*` entre elles : 1. `pattern:-?\d+(\.\d+)?` - le premier nombre, 1. `pattern:[-+*/]` - l'opérateur, 1. `pattern:-?\d+(\.\d+)?` - le deuxième nombre.
To make each of these parts a separate element of the result array, let's enclose them in parentheses: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. Pour faire de chacune de ces parties un élément distinct du tableau de correspondance, entourons-les de parenthèses : `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.
In action: Cela donne :
```js run let regexp = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
alert( "1.2 + 12".match(regexp) ); ```
The result includes: Le résultat inclus :
- `result[0] == "1.2 + 12"` (full match) - `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part) - `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part) - `result[3] == "+"` (third group `([-+*\/])` -- the operator) - `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number) - `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined) - `result[0] == "1.2 + 12"` (la correspondance complète) - `result[1] == "1.2"` (premier groupe `(-?\d+(\.\d+)?)` -- le premier nombre, avec la partie décimale) - `result[2] == ".2"` (second groupe`(\.\d+)?` -- la première partie décimale) - `result[3] == "+"` (troisième groupe `([-+*\/])` -- l'opérateur) - `result[4] == "12"` (quatrième groupe `(-?\d+(\.\d+)?)` -- le second nombre) - `result[5] == undefined` (cinquième groupe `(\.\d+)?` -- la deuxième partie décimale est absente, c'est non défini)
We only want the numbers and the operator, without the full match or the decimal parts, so let's "clean" the result a bit. Nous ne souhaitons que les nombres et l'opérateur, sans la correspondance entière, ni les parties décimales. Faisons alors un peu le ménage.
The full match (the arrays first item) can be removed by shifting the array `result.shift()`. La correspondance complète(le premier élément du tableau) peut être enlevée par `result.shift()`.
Groups that contain decimal parts (number 2 and 4) `pattern:(.\d+)` can be excluded by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`. Les groupes contenant les parties décimales(groupes 2 et 4) `pattern:(.\d+)` peuvent être exclus en ajoutant `pattern:?:` au début : `pattern:(?:\.\d+)?`.
The final solution: La solution complète :
```js run function parse(expr) { Expand Down