Skip to content

Commit 91e4c89

Browse files
committed
up
1 parent 18fb1b0 commit 91e4c89

File tree

4 files changed

+77
-68
lines changed

4 files changed

+77
-68
lines changed

10-regular-expressions-javascript/09-regexp-groups/2-parse-expression/solution.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

10-regular-expressions-javascript/09-regexp-groups/2-parse-expression/task.md

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks.
2+
3+
An operator is `pattern:[-+*/]`. We put a dash `pattern:-` the first, because in the middle it would mean a character range, we don't need that.
4+
5+
Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`.
6+
7+
We need a number, an operator, and then another number. And optional spaces between them.
8+
9+
The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
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+)?)`.
12+
13+
In action:
14+
15+
```js run
16+
let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
17+
18+
alert( "1.2 + 12".match(reg) );
19+
```
20+
21+
The result includes:
22+
23+
- `result[0] == "1.2 + 12"` (full match)
24+
- `result[1] == "1"` (first parentheses)
25+
- `result[2] == "2"` (second parentheses -- the decimal part `(\.\d+)?`)
26+
- `result[3] == "+"` (...)
27+
- `result[4] == "12"` (...)
28+
- `result[5] == undefined` (the last decimal part is absent, so it's undefined)
29+
30+
We need only numbers and the operator. We don't need decimal parts.
31+
32+
So let's remove extra groups from capturing by added `pattern:?:`, for instance: `pattern:(?:\.\d+)?`.
33+
34+
The final solution:
35+
36+
```js run
37+
function parse(expr) {
38+
let reg = /(-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?)/;
39+
40+
let result = expr.match(reg);
41+
42+
if (!result) return;
43+
result.shift();
44+
45+
return result;
46+
}
47+
48+
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45
49+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Parse an expression
2+
3+
An arithmetical expression consists of 2 numbers and an operator between them, for instance:
4+
5+
- `1 + 2`
6+
- `1.2 * 3.4`
7+
- `-3 / -6`
8+
- `-2 - 2`
9+
10+
The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
11+
12+
There may be extra spaces at the beginning, at the end or between the parts.
13+
14+
Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
15+
16+
1. The first number.
17+
2. The operator.
18+
3. The second number.
19+
20+
For example:
21+
22+
```js
23+
let [a, op, b] = parse("1.2 * 3.4");
24+
25+
alert(a); // 1.2
26+
alert(op); // *
27+
alert(b); // 3.4
28+
```

0 commit comments

Comments
 (0)