You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 9-regular-expressions/11-regexp-groups/04-parse-expression/solution.md
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,8 @@ The full regular expression: `pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`.
10
10
11
11
It has 3 parts, with `pattern:\s*` between them:
12
12
1.`pattern:-?\d+(\.\d+)?` - the first number,
13
-
1.`pattern:[-+*/]` - the operator,
14
-
1.`pattern:-?\d+(\.\d+)?` - the second number.
13
+
2.`pattern:[-+*/]` - the operator,
14
+
3.`pattern:-?\d+(\.\d+)?` - the second number.
15
15
16
16
To make each of these parts a separate element of the result array, let's enclose them in parentheses: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.
17
17
@@ -54,3 +54,17 @@ function parse(expr) {
54
54
55
55
alert( parse("-1.23 * 3.45") ); // -1.23, *, 3.45
56
56
```
57
+
58
+
As an alternative to using the non-capturing `?:`, we could name the groups, like this:
59
+
60
+
```js run
61
+
functionparse(expr) {
62
+
let regexp =/(?<a>-?\d+(?:\.\d+)?)\s*(?<operator>[-+*\/])\s*(?<b>-?\d+(?:\.\d+)?)/;
0 commit comments