Skip to content

Commit 7feada9

Browse files
committed
fourth task
1 parent 4c95e77 commit 7feada9

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
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.
22

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 `-`.
44

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.
66

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.
88

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+)?`.
1010

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.
1515

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+)?)`.
1717

18-
In action:
18+
Cela donne :
1919

2020
```js run
2121
let regexp = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
2222

2323
alert( "1.2 + 12".match(regexp) );
2424
```
2525

26-
The result includes:
26+
Le résultat inclus :
2727

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)
3434

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.
3636

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()`.
3838

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+)?`.
4040

41-
The final solution:
41+
La solution complète :
4242

4343
```js run
4444
function parse(expr) {

9-regular-expressions/11-regexp-groups/04-parse-expression/task.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Parse an expression
1+
# Parsez une expression
22

3-
An arithmetical expression consists of 2 numbers and an operator between them, for instance:
3+
Une expression arithmétique consiste en 2 nombres et un opérateur entre les deux, par exemple :
44

55
- `1 + 2`
66
- `1.2 * 3.4`
77
- `-3 / -6`
88
- `-2 - 2`
99

10-
The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
10+
L'opérateur l'un des : `"+"`, `"-"`, `"*"` ou `"/"`.
1111

12-
There may be extra spaces at the beginning, at the end or between the parts.
12+
Il peut y avoir des espaces supplémentaires au début, à la fin ou entre chaque partie.
1313

14-
Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
14+
Créez une fonction `parse(expr)` qui prend une expression et retourne un tableau de trois éléments :
1515

16-
1. The first number.
17-
2. The operator.
18-
3. The second number.
16+
1. Le premier nombre.
17+
2. L'opérateur.
18+
3. Le second nombre.
1919

20-
For example:
20+
Par exemple :
2121

2222
```js
2323
let [a, op, b] = parse("1.2 * 3.4");

0 commit comments

Comments
 (0)