Skip to content

Commit 962fa79

Browse files
committed
closes #3122
1 parent f0f4366 commit 962fa79

File tree

1 file changed

+14
-0
lines changed
  • 9-regular-expressions/11-regexp-groups/04-parse-expression

1 file changed

+14
-0
lines changed

9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,17 @@ function parse(expr) {
5454

5555
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45
5656
```
57+
58+
As an alternative to using the non-capturing `?:`, we could name the groups, like this:
59+
60+
```js run
61+
function parse(expr) {
62+
let regexp = /(?<a>-?\d+(?:\.\d+)?)\s*(?<operator>[-+*\/])\s*(?<b>-?\d+(?:\.\d+)?)/;
63+
64+
let result = expr.match(regexp);
65+
66+
return [result.groups.a, result.groups.operator, result.groups.b];
67+
}
68+
69+
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45;
70+
```

0 commit comments

Comments
 (0)