This commit is contained in:
Ilya Kantor 2019-06-25 16:25:50 +03:00
parent 69fbb63bc4
commit 4f2a90854d
4 changed files with 33 additions and 26 deletions

View file

@ -1,11 +1,11 @@
An non-negative integer number is `pattern:\d+`. We should exclude `0` as the first digit, as we don't need zero, but we can allow it in further digits.
An non-negative integer number is `pattern:\d+`. A zero `0` can't be the first digit, but we should allow it in further digits.
So that gives us `pattern:[1-9]\d*`.
A decimal part is: `pattern:\.\d+`.
Because the decimal part is optional, let's put it in parentheses with the quantifier `pattern:'?'`.
Because the decimal part is optional, let's put it in parentheses with the quantifier `pattern:?`.
Finally we have the regexp: `pattern:[1-9]\d*(\.\d+)?`:

View file

@ -1,8 +1,10 @@
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.
An operator is `pattern:[-+*/]`.
Note that a slash should be escaped inside a JavaScript regexp `pattern:/.../`.
Please note:
- Here the dash `pattern:-` goes first in the brackets, because in the middle it would mean a character range, while we just want a character `-`.
- A slash `/` should be escaped inside a JavaScript regexp `pattern:/.../`, we'll do that later.
We need a number, an operator, and then another number. And optional spaces between them.

View file

@ -47,13 +47,13 @@ In this example parentheses were used to make a group for repeating `pattern:(..
## Contents of parentheses
Parentheses are numbered from left to right. The search engine remembers the content of each and allows to reference it in the pattern or in the replacement string.
Parentheses are numbered from left to right. The search engine remembers the content matched by each of them and allows to reference it in the pattern or in the replacement string.
For instance, we'd like to find HTML tags `pattern:<.*?>`, and process them.
Let's wrap the inner content into parentheses, like this: `pattern:<(.*?)>`.
We'll get them into an array:
We'll get both the tag as a whole and its content as an array:
```js run
let str = '<h1>Hello, world!</h1>';
@ -62,7 +62,7 @@ let reg = /<(.*?)>/;
alert( str.match(reg) ); // Array: ["<h1>", "h1"]
```
The call to [String#match](mdn:js/String/match) returns groups only if the regexp has no `pattern:/.../g` flag.
The call to [String#match](mdn:js/String/match) returns groups only if the regexp only looks for the first match, that is: has no `pattern:/.../g` flag.
If we need all matches with their groups then we can use `.matchAll` or `regexp.exec` as described in <info:regexp-methods>:
@ -162,9 +162,9 @@ alert(groups.day); // 30
As you can see, the groups reside in the `.groups` property of the match.
We can also use them in replacements, as `pattern:$<name>` (like `$1..9`, but name instead of a digit).
We can also use them in the replacement string, as `pattern:$<name>` (like `$1..9`, but a name instead of a digit).
For instance, let's rearrange the date into `day.month.year`:
For instance, let's reformat the date into `day.month.year`:
```js run
let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/;
@ -176,7 +176,7 @@ let rearranged = str.replace(dateRegexp, '$<day>.$<month>.$<year>');
alert(rearranged); // 30.04.2019
```
If we use a function, then named `groups` object is always the last argument:
If we use a function for the replacement, then named `groups` object is always the last argument:
```js run
let dateRegexp = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/;
@ -231,7 +231,12 @@ alert( result[1] ); // John
## Summary
- Parentheses can be:
- capturing `(...)`, ordered left-to-right, accessible by number.
- named capturing `(?<name>...)`, accessible by name.
- non-capturing `(?:...)`, used only to apply quantifier to the whole groups.
Parentheses group together a part of the regular expression, so that the quantifier applies to it as a whole.
Parentheses groups are numbered left-to-right, and can optionally be named with `(?<name>...)`.
The content, matched by a group, can be referenced both in the replacement string as `$1`, `$2` etc, or by the name `$name` if named.
So, parentheses groups are called "capturing groups", as they "capture" a part of the match. We get that part separately from the result.
We can exclude the group from remembering (make in "non-capturing") by putting `?:` at the start: `(?:...)`, that's used if we'd like to apply a quantifier to the whole group, but don't need it in the result.