Skip to content

Commit 681cae4

Browse files
committed
regexp
1 parent 2054757 commit 681cae4

File tree

16 files changed

+505
-362
lines changed

16 files changed

+505
-362
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
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)
21+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Check MAC-address
2+
3+
[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon.
4+
5+
For instance: `subject:'01:32:54:67:89:AB'`.
6+
7+
Write a regexp that checks whether a string is MAC-address.
8+
9+
Usage:
10+
```js
11+
let reg = /your regexp/;
12+
13+
alert( reg.test('01:32:54:67:89:AB') ); // true
14+
15+
alert( reg.test('0132546789AB') ); // false (no colons)
16+
17+
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
18+
19+
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
20+
```

9-regular-expressions/11-regexp-groups/1-find-webcolor-3-or-6/solution.md renamed to 9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/solution.md

File renamed without changes.

9-regular-expressions/11-regexp-groups/1-find-webcolor-3-or-6/task.md renamed to 9-regular-expressions/11-regexp-groups/02-find-webcolor-3-or-6/task.md

File renamed without changes.

9-regular-expressions/11-regexp-groups/2-find-decimal-numbers/solution.md renamed to 9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md

File renamed without changes.

9-regular-expressions/11-regexp-groups/2-find-decimal-numbers/task.md renamed to 9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/task.md

File renamed without changes.

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

File renamed without changes.

9-regular-expressions/11-regexp-groups/5-parse-expression/task.md renamed to 9-regular-expressions/11-regexp-groups/04-parse-expression/task.md

File renamed without changes.

9-regular-expressions/11-regexp-groups/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ That regexp is not perfect, but mostly works and helps to fix accidental mistype
6565

6666
## Parentheses contents in the match
6767

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.
6969

7070
The method `str.match(regexp)`, if `regexp` has no flag `g`, looks for the first match and returns it as an array:
7171

@@ -347,4 +347,4 @@ If the parentheses have no name, then their contents is available in the match a
347347

348348
We can also use parentheses contents in the replacement string in `str.replace`: by the number `$n` or the name `$<name>`.
349349

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.

9-regular-expressions/12-regexp-backreferences/article.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
# Backreferences in pattern: \n and \k
1+
# Backreferences in pattern: \N and \k<name>
22

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.
44

5-
## Backreference by number: \n
5+
## Backreference by number: \N
66

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.
88

9-
To make things clear let's consider a task.
9+
To make clear why that's helpful, let's consider a task.
1010

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.
1212

13-
How to look for them?
13+
How to find them?
1414

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!"`:
1616

1717
```js run
1818
let str = `He said: "She's the one!".`;
1919

2020
let reg = /['"](.*?)['"]/g;
2121

22-
// The result is not what we expect
22+
// The result is not what we'd like to have
2323
alert( str.match(reg) ); // "She'
2424
```
2525

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.
2727

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`.
2929

3030
Here's the correct code:
3131

@@ -39,20 +39,27 @@ let reg = /(['"])(.*?)\1/g;
3939
alert( str.match(reg) ); // "She's the one!"
4040
```
4141

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.
4343

4444
Further in the pattern `pattern:\1` means "find the same text as in the first group", exactly the same quote in our case.
4545

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.
4747

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+
```
5055
5156
## Backreference by name: `\k<name>`
5257
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<имя>`.
5461
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>`:
5663
5764
```js run
5865
let str = `He said: "She's the one!".`;

0 commit comments

Comments
 (0)