components
This commit is contained in:
parent
304d578b54
commit
6fb4aabcba
344 changed files with 669 additions and 406 deletions
|
@ -0,0 +1,51 @@
|
|||
A regexp for a number is: `pattern:-?\d+(\.\d+)?`. We created it in previous tasks.
|
||||
|
||||
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:/.../`.
|
||||
|
||||
We need a number, an operator, and then another number. And optional spaces between them.
|
||||
|
||||
The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
|
||||
|
||||
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+)?)`.
|
||||
|
||||
In action:
|
||||
|
||||
```js run
|
||||
let reg = /(-?\d+(\.\d+)?)\s*([-+*\/])\s*(-?\d+(\.\d+)?)/;
|
||||
|
||||
alert( "1.2 + 12".match(reg) );
|
||||
```
|
||||
|
||||
The result includes:
|
||||
|
||||
- `result[0] == "1.2 + 12"` (full match)
|
||||
- `result[1] == "1.2"` (first group `(-?\d+(\.\d+)?)` -- the first number, including the decimal part)
|
||||
- `result[2] == ".2"` (second group`(\.\d+)?` -- the first decimal part)
|
||||
- `result[3] == "+"` (third group `([-+*\/])` -- the operator)
|
||||
- `result[4] == "12"` (forth group `(-?\d+(\.\d+)?)` -- the second number)
|
||||
- `result[5] == undefined` (fifth group `(\.\d+)?` -- the last decimal part is absent, so it's undefined)
|
||||
|
||||
We only want the numbers and the operator, without the full match or the decimal parts.
|
||||
|
||||
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:
|
||||
|
||||
```js run
|
||||
function parse(expr) {
|
||||
let reg = /(-?\d+(?:\.\d+)?)\s*([-+*\/])\s*(-?\d+(?:\.\d+)?)/;
|
||||
|
||||
let result = expr.match(reg);
|
||||
|
||||
if (!result) return [];
|
||||
result.shift();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45
|
||||
```
|
|
@ -0,0 +1,28 @@
|
|||
# Parse an expression
|
||||
|
||||
An arithmetical expression consists of 2 numbers and an operator between them, for instance:
|
||||
|
||||
- `1 + 2`
|
||||
- `1.2 * 3.4`
|
||||
- `-3 / -6`
|
||||
- `-2 - 2`
|
||||
|
||||
The operator is one of: `"+"`, `"-"`, `"*"` or `"/"`.
|
||||
|
||||
There may be extra spaces at the beginning, at the end or between the parts.
|
||||
|
||||
Create a function `parse(expr)` that takes an expression and returns an array of 3 items:
|
||||
|
||||
1. The first number.
|
||||
2. The operator.
|
||||
3. The second number.
|
||||
|
||||
For example:
|
||||
|
||||
```js
|
||||
let [a, op, b] = parse("1.2 * 3.4");
|
||||
|
||||
alert(a); // 1.2
|
||||
alert(op); // *
|
||||
alert(b); // 3.4
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue