1
1
A regexp for a number is: ` pattern:-?\d+(\.\d+)? ` . We created it in previous tasks.
2
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.
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.
4
4
5
5
Note that a slash should be escaped inside a JavaScript regexp ` pattern:/.../ ` .
6
6
@@ -21,15 +21,17 @@ alert( "1.2 + 12".match(reg) );
21
21
The result includes:
22
22
23
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)
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)
29
29
30
- We need only numbers and the operator. We don't need decimal parts.
30
+ We only want the numbers and the operator, without the full match or the decimal parts.
31
31
32
- So let's remove extra groups from capturing by added ` pattern:?: ` , for instance: ` pattern:(?:\.\d+)? ` .
32
+ The full match (the arrays first item) can be removed by shifting the array ` pattern:result.shift() ` .
33
+
34
+ The decimal groups can be removed by making them into non-capturing groups, by adding ` pattern:?: ` to the beginning: ` pattern:(?:\.\d+)? ` .
33
35
34
36
The final solution:
35
37
0 commit comments