|
1 | | -A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks. |
| 1 | +Регэксп для числа: `pattern:-?\d+(\.\d+)?`. Мы создали его в предыдущих задачах. |
2 | 2 |
|
3 | | -An operator is `pattern:[-+*/]`. We put the dash `pattern:-` first, because in the middle it would mean a character range, we don't need that. |
| 3 | +Регэксп для оператора `pattern:[-+*/]`. Мы вставили тире `pattern:-` в начале, потому что в середине этот символ будет означать диапазон, а нам это не нужно. |
4 | 4 |
|
5 | | -Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`. |
| 5 | +Отметим, что косая черта должна быть экранированна внутри регэксп JavaScript `pattern:/.../`. |
6 | 6 |
|
7 | | -We need a number, an operator, and then another number. And optional spaces between them. |
| 7 | +Нам необходимо число, оператор и, затем, другие числа. И необязательные символы пробела между ними. |
8 | 8 |
|
9 | | -The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. |
| 9 | +Полное выражение: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`. |
10 | 10 |
|
11 | | -To get a result as an array let's put parentheses around the data that we need: numbers and the operator: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. |
| 11 | +Для получения результата в виде массива давайте вставим скобки вокруг данных, которые нам необходимы: чисел и операторов: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`. |
12 | 12 |
|
13 | | -In action: |
| 13 | +В действии: |
14 | 14 |
|
15 | 15 | ```js run |
16 | 16 | let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/; |
17 | 17 |
|
18 | 18 | alert( "1.2 + 12".match(reg) ); |
19 | 19 | ``` |
20 | 20 |
|
21 | | -The result includes: |
| 21 | +Результат включает: |
22 | 22 |
|
23 | | -- `result[0] == "1.2 + 12"` (full match) |
24 | | -- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part) |
25 | | -- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part) |
26 | | -- `result[3] == "+"` (third group `([-+*\/])` -- the operator) |
27 | | -- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number) |
28 | | -- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined) |
| 23 | +- `result[0] == "1.2 + 12"` (полное совпадение) |
| 24 | +- `result[1] == "1.2"` (первая группа `(-?\d+(\.\d+)?)` -- первое число, включая десятичную часть) |
| 25 | +- `result[2] == ".2"` (вторая группа `(\.\d+)?` -- первая десятичная часть) |
| 26 | +- `result[3] == "+"` (третья группа `([-+*\/])` -- оператор) |
| 27 | +- `result[4] == "12"` (чертвертая группа `(-?\d+(\.\d+)?)` -- второе число) |
| 28 | +- `result[5] == undefined` (пятая группа `(\.\d+)?` -- вторая десятичная часть отсутствует, поэтому значение `undefined`) |
29 | 29 |
|
30 | | -We only want the numbers and the operator, without the full match or the decimal parts. |
| 30 | +Нам необходимы только числа и оператор без полного совпадения или десятичной части. |
31 | 31 |
|
32 | | -The full match (the arrays first item) can be removed by shifting the array `pattern:result.shift()`. |
| 32 | +Полное совпадение (первый элемент массива) может быть удален при помощи сдвига массива `pattern:result.shift()`. |
33 | 33 |
|
34 | | -The decimal groups can be removed by making them into non-capturing groups, by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`. |
| 34 | +От десятичных групп можно избавиться, если исключить захват скобочной группы добавив `pattern:?:` в начало: `pattern:(?:\.\d+)?`. |
35 | 35 |
|
36 | | -The final solution: |
| 36 | +Итоговое решение: |
37 | 37 |
|
38 | 38 | ```js run |
39 | 39 | function parse(expr) { |
|
0 commit comments