|
1 | | -A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in the previous task. |
| 1 | +Une regexp pour un nombre : `pattern:-?\d+(\.\d+)?`. Nous l'avons vu dans l'exercice précédent. |
2 | 2 |
|
3 | | -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 `-`. |
| 3 | +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 `-`. |
4 | 4 |
|
5 | | -The slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later. |
| 5 | +Le slash `/` doit être échappé en javascript dans une regexp `pattern:/.../`, ce que nous ferons plus tard. |
6 | 6 |
|
7 | | -We need a number, an operator, and then another number. And optional spaces between them. |
| 7 | +Nous cherchons un nombre, un opérateur puis un autre nombre. Et d'éventuels espaces entre eux. |
8 | 8 |
|
9 | | -The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. |
| 9 | +Cela done l'expression régulière : `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. |
10 | 10 |
|
11 | | -It has 3 parts, with `pattern:\s*` between them: |
12 | | -1. `pattern:-?\d+(\.\d+)?` - the first number, |
13 | | -1. `pattern:[-+*/]` - the operator, |
14 | | -1. `pattern:-?\d+(\.\d+)?` - the second number. |
| 11 | +Il y a trois parties, avec `pattern:\s*` entre elles : |
| 12 | +1. `pattern:-?\d+(\.\d+)?` - le premier nombre, |
| 13 | +1. `pattern:[-+*/]` - l'opérateur, |
| 14 | +1. `pattern:-?\d+(\.\d+)?` - le deuxième nombre. |
15 | 15 |
|
16 | | -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+)?)`. |
| 16 | +Pour rendre chacune des ces parties un élément distinct du tableau de correspondance, entourons-les de parenthèses : `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. |
17 | 17 |
|
18 | | -In action: |
| 18 | +Cela donne : |
19 | 19 |
|
20 | 20 | ```js run |
21 | 21 | let regexp = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/; |
22 | 22 |
|
23 | 23 | alert( "1.2 + 12".match(regexp) ); |
24 | 24 | ``` |
25 | 25 |
|
26 | | -The result includes: |
| 26 | +Le résultat inclus : |
27 | 27 |
|
28 | | -- `result[0] == "1.2 + 12"` (full match) |
29 | | -- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part) |
30 | | -- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part) |
31 | | -- `result[3] == "+"` (third group `([-+*\/])` -- the operator) |
32 | | -- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number) |
33 | | -- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined) |
| 28 | +- `result[0] == "1.2 + 12"` (la correspondance complète) |
| 29 | +- `result[1] == "1.2"` (premier groupe `(-?\d+(\.\d+)?)` -- le premier nombre, avec la partie décimale) |
| 30 | +- `result[2] == ".2"` (second groupe`(\.\d+)?` -- la première partie décimale) |
| 31 | +- `result[3] == "+"` (troisième groupe `([-+*\/])` -- l'opérateur) |
| 32 | +- `result[4] == "12"` (quatrième groupe `(-?\d+(\.\d+)?)` -- le second nombre) |
| 33 | +- `result[5] == undefined` (cinquième groupe `(\.\d+)?` -- la dernière partie décimale est absente, c'est non défini) |
34 | 34 |
|
35 | | -We only want the numbers and the operator, without the full match or the decimal parts, so let's "clean" the result a bit. |
| 35 | +Nous ne souhaitons que les nombres et l'opérateur, sans la correspondance entière ni les parties décimales, alors faisons un peu le ménage. |
36 | 36 |
|
37 | | -The full match (the arrays first item) can be removed by shifting the array `result.shift()`. |
| 37 | +La correspondance complète(le premier élément du tableau) peut être enlevé par `result.shift()`. |
38 | 38 |
|
39 | | -Groups that contain decimal parts (number 2 and 4) `pattern:(.\d+)` can be excluded by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`. |
| 39 | +Les groupes contenant les parties décimales(groupes 2 et 4) `pattern:(.\d+)` peuvent être exclus en ajoutant `pattern:?:` au début : `pattern:(?:\.\d+)?`. |
40 | 40 |
|
41 | | -The final solution: |
| 41 | +La solution complète : |
42 | 42 |
|
43 | 43 | ```js run |
44 | 44 | function parse(expr) { |
|
0 commit comments