regexp
This commit is contained in:
parent
20547570ff
commit
681cae4b6a
16 changed files with 505 additions and 362 deletions
|
@ -0,0 +1,21 @@
|
|||
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the flag `pattern:i` is set).
|
||||
|
||||
We need that number `NN`, and then `:NN` repeated 5 times (more numbers);
|
||||
|
||||
The regexp is: `pattern:[0-9a-f]{2}(:[0-9a-f]{2}){5}`
|
||||
|
||||
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:^...$`.
|
||||
|
||||
Finally:
|
||||
|
||||
```js run
|
||||
let reg = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
|
||||
|
||||
alert( reg.test('01:32:54:67:89:AB') ); // true
|
||||
|
||||
alert( reg.test('0132546789AB') ); // false (no colons)
|
||||
|
||||
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, need 6)
|
||||
|
||||
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ in the end)
|
||||
```
|
20
9-regular-expressions/11-regexp-groups/01-test-mac/task.md
Normal file
20
9-regular-expressions/11-regexp-groups/01-test-mac/task.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# Check MAC-address
|
||||
|
||||
[MAC-address](https://en.wikipedia.org/wiki/MAC_address) of a network interface consists of 6 two-digit hex numbers separated by a colon.
|
||||
|
||||
For instance: `subject:'01:32:54:67:89:AB'`.
|
||||
|
||||
Write a regexp that checks whether a string is MAC-address.
|
||||
|
||||
Usage:
|
||||
```js
|
||||
let reg = /your regexp/;
|
||||
|
||||
alert( reg.test('01:32:54:67:89:AB') ); // true
|
||||
|
||||
alert( reg.test('0132546789AB') ); // false (no colons)
|
||||
|
||||
alert( reg.test('01:32:54:67:89') ); // false (5 numbers, must be 6)
|
||||
|
||||
alert( reg.test('01:32:54:67:89:ZZ') ) // false (ZZ ad the end)
|
||||
```
|
|
@ -65,7 +65,7 @@ That regexp is not perfect, but mostly works and helps to fix accidental mistype
|
|||
|
||||
## Parentheses contents in the match
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
The method `str.match(regexp)`, if `regexp` has no flag `g`, looks for the first match and returns it as an array:
|
||||
|
||||
|
@ -347,4 +347,4 @@ If the parentheses have no name, then their contents is available in the match a
|
|||
|
||||
We can also use parentheses contents in the replacement string in `str.replace`: by the number `$n` or the name `$<name>`.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue