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
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the flag `pattern:i` is set).
2
+
3
+
We need that number `NN`, and then `:NN` repeated 5 times (more numbers);
4
+
5
+
The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
6
+
7
+
Now let's show that the match should capture all the text: start at the beginning and end at the end. That's done by wrapping the pattern in `pattern:^...$`.
8
+
9
+
Finally:
10
+
11
+
```js run
12
+
let reg =/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
13
+
14
+
alert( reg.test('01:32:54:67:89:AB') ); // true
15
+
16
+
alert( reg.test('0132546789AB') ); // false (no colons)
17
+
18
+
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6)
19
+
20
+
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
Copy file name to clipboardExpand all lines: 9-regular-expressions/11-regexp-groups/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ That regexp is not perfect, but mostly works and helps to fix accidental mistype
65
65
66
66
## Parentheses contents in the match
67
67
68
-
Parentheses are numbered from left to right. The search engine remembers the content matched by each of them and allows to get it in the result.
68
+
Parentheses are numbered from left to right. The search engine memorizes the content matched by each of them and allows to get it in the result.
69
69
70
70
The method `str.match(regexp)`, if `regexp` has no flag `g`, looks for the first match and returns it as an array:
71
71
@@ -347,4 +347,4 @@ If the parentheses have no name, then their contents is available in the match a
347
347
348
348
We can also use parentheses contents in the replacement string in `str.replace`: by the number `$n` or the name `$<name>`.
349
349
350
-
A group may be excluded from remembering by adding `pattern:?:` in its start. That's used when we need to apply a quantifier to the whole group, but don't remember it as a separate item in the results array. We also can't reference such parentheses in the replacement string.
350
+
A group may be excluded from numbering by adding `pattern:?:` in its start. That's used when we need to apply a quantifier to the whole group, but don't want it as a separate item in the results array. We also can't reference such parentheses in the replacement string.
Copy file name to clipboardExpand all lines: 9-regular-expressions/12-regexp-backreferences/article.md
+24-17Lines changed: 24 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,31 @@
1
-
# Backreferences in pattern: \n and \k
1
+
# Backreferences in pattern: \N and \k<name>
2
2
3
-
We can use the contents of capturing groups `(...)` not only in the result or in the replacement string, but also in the pattern itself.
3
+
We can use the contents of capturing groups `pattern:(...)` not only in the result or in the replacement string, but also in the pattern itself.
4
4
5
-
## Backreference by number: \n
5
+
## Backreference by number: \N
6
6
7
-
A group can be referenced in the pattern using `\n`, where `n` is the group number.
7
+
A group can be referenced in the pattern using `pattern:\N`, where `N` is the group number.
8
8
9
-
To make things clear let's consider a task.
9
+
To make clear why that's helpful, let's consider a task.
10
10
11
-
We need to find a quoted string: either a single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants need to match.
11
+
We need to find quoted strings: either single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants should match.
12
12
13
-
How to look for them?
13
+
How to find them?
14
14
15
-
We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like the string `subject:"She's the one!"`:
15
+
We can put both kinds of quotes in the square brackets: `pattern:['"](.*?)['"]`, but it would find strings with mixed quotes, like `match:"...'` and `match:'..."`. That would lead to incorrect matches when one quote appears inside other ones, like in the string `subject:"She's the one!"`:
16
16
17
17
```js run
18
18
let str =`He said: "She's the one!".`;
19
19
20
20
let reg =/['"](.*?)['"]/g;
21
21
22
-
// The result is not what we expect
22
+
// The result is not what we'd like to have
23
23
alert( str.match(reg) ); // "She'
24
24
```
25
25
26
-
As we can see, the pattern found an opening quote `match:"`, then the text is consumed lazily till the other quote `match:'`, that closes the match.
26
+
As we can see, the pattern found an opening quote `match:"`, then the text is consumed till the other quote `match:'`, that closes the match.
27
27
28
-
To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can wrap it into a capturing group and use the backreference.
28
+
To make sure that the pattern looks for the closing quote exactly the same as the opening one, we can wrap it into a capturing group and backreference it: `pattern:(['"])(.*?)\1`.
29
29
30
30
Here's the correct code:
31
31
@@ -39,20 +39,27 @@ let reg = /(['"])(.*?)\1/g;
39
39
alert( str.match(reg) ); // "She's the one!"
40
40
```
41
41
42
-
Now it works! The regular expression engine finds the first quote `pattern:(['"])` and remembers the content of `pattern:(...)`, that's the first capturing group.
42
+
Now it works! The regular expression engine finds the first quote `pattern:(['"])` and memorizes its content. That's the first capturing group.
43
43
44
44
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
45
45
46
-
Please note:
46
+
Similar to that, `pattern:\2` would mean the contents of the second group, `pattern:\3` - the 3rd group, and so on.
47
47
48
-
- To reference a group inside a replacement string -- we use `$1`, while in the pattern -- a backslash `\1`.
49
-
- If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not remembered by the engine.
48
+
```smart
49
+
If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not memorized by the engine.
50
+
```
51
+
52
+
```warn header="Don't mess up: in the pattern `pattern:\1`, in the replacement: `pattern:$1`"
53
+
In the replacement string we use a dollar sign: `pattern:$1`, while in the pattern - a backslash `pattern:\1`.
54
+
```
50
55
51
56
## Backreference by name: `\k<name>`
52
57
53
-
For named groups, we can backreference by `\k<name>`.
58
+
If a regexp has many parentheses, it's convenient to give them names.
59
+
60
+
To reference a named group we can use `pattern:\k<имя>`.
54
61
55
-
The same example with the named group:
62
+
In the example below the group with quotes is named `pattern:?<quote>`, so the backreference is `pattern:\k<quote>`:
0 commit comments