Skip to content

Commit 78b1918

Browse files
authored
Update solution.md
1 parent c4f9c0f commit 78b1918

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

  • 9-regular-expressions/09-regexp-groups/5-parse-expression

9-regular-expressions/09-regexp-groups/5-parse-expression/solution.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks.
1+
Регэксп для числа: `pattern:-?\d+(\.\d+)?`. Мы создали его в предыдущих задачах.
22

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:-` в начале, потому что в середине этот символ будет означать диапазон, а нам это не нужно.
44

5-
Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`.
5+
Отметим, что косая черта должна быть экранированна внутри регэксп JavaScript `pattern:/.../`.
66

7-
We need a number, an operator, and then another number. And optional spaces between them.
7+
Нам необходимо число, оператор и, затем, другие числа. И необязательные символы пробела между ними.
88

9-
The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
9+
Полное выражение: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
1010

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

13-
In action:
13+
В действии:
1414

1515
```js run
1616
let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
1717

1818
alert( "1.2 + 12".match(reg) );
1919
```
2020

21-
The result includes:
21+
Результат включает:
2222

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`)
2929

30-
We only want the numbers and the operator, without the full match or the decimal parts.
30+
Нам необходимы только числа и оператор без полного совпадения или десятичной части.
3131

32-
The full match (the arrays first item) can be removed by shifting the array `pattern:result.shift()`.
32+
Полное совпадение (первый элемент массива) может быть удален при помощи сдвига массива `pattern:result.shift()`.
3333

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

36-
The final solution:
36+
Итоговое решение:
3737

3838
```js run
3939
function parse(expr) {

0 commit comments

Comments
 (0)