This commit is contained in:
Ilya Kantor 2022-08-14 12:31:05 +03:00
parent f0f43667a4
commit 962fa79b96

View file

@ -54,3 +54,17 @@ function parse(expr) {
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45
```
As an alternative to using the non-capturing `?:`, we could name the groups, like this:
```js run
function parse(expr) {
let regexp = /(?<a>-?\d+(?:\.\d+)?)\s*(?<operator>[-+*\/])\s*(?<b>-?\d+(?:\.\d+)?)/;
let result = expr.match(regexp);
return [result.groups.a, result.groups.operator, result.groups.b];
}
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45;
```