Fix and add details to result array explanation

This commit is contained in:
duianto 2018-11-21 22:52:49 +01:00 committed by GitHub
parent 924ec1e19f
commit 0e2c41ce36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks. A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks.
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. 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.
Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`. Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`.
@ -21,15 +21,17 @@ alert( "1.2 + 12".match(reg) );
The result includes: The result includes:
- `result[0] == "1.2 + 12"` (full match) - `result[0] == "1.2 + 12"` (full match)
- `result[1] == "1"` (first parentheses) - `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part)
- `result[2] == "2"` (second parentheses -- the decimal part `(\.\d+)?`) - `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part)
- `result[3] == "+"` (...) - `result[3] == "+"` (third group `([-+*\/])` -- the operator)
- `result[4] == "12"` (...) - `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number)
- `result[5] == undefined` (the last decimal part is absent, so it's undefined) - `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined)
We need only numbers and the operator. We don't need decimal parts. We only want the numbers and the operator, without the full match or the decimal parts.
So let's remove extra groups from capturing by added `pattern:?:`, for instance: `pattern:(?:\.\d+)?`. The full match (the arrays first item) can be removed by shifting the array `pattern:result.shift()`.
The decimal groups can be removed by making them into non-capturing groups, by adding `pattern:?:` to the beginning: `pattern:(?:\.\d+)?`.
The final solution: The final solution: