This commit is contained in:
Ilya Kantor 2017-03-20 20:52:29 +03:00
parent 1e2b09b6fb
commit 7ddea43ab4
22 changed files with 382 additions and 343 deletions

View file

@ -0,0 +1,6 @@
The empty string is the only match: it starts and immediately finishes.
The task once again demonstrates that anchors are not characters, but tests.
The string is empty `""`. The engine first matches the `pattern:^` (input start), yes it's there, and then immediately the end `pattern:$`, it's here too. So there's a match.

View file

@ -0,0 +1,3 @@
# Regexp ^$
Which string matches the pattern `pattern:^$`?

View file

@ -0,0 +1,21 @@
A two-digit hex number is `pattern:[0-9a-f]{2}` (assuming the `pattern:i` flag is enabled).
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)
```

View 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)
```

View file

@ -0,0 +1,57 @@
# String start ^ and finish $
The caret `pattern:'^'` and dollar `pattern:'$'` characters have special meaning in a regexp. They are called "anchors".
[cut]
The caret `pattern:^` matches at the end of the text, and the dollar `pattern:$` -- in the end.
For instance, let's test if the text starts with `Mary`:
```js run
let str1 = 'Mary had a little lamb, it's fleece was white as snow';
let str2 = 'Everywhere Mary went, the lamp was sure to go';
alert( /^Mary/.test(str1) ); // true
alert( /^Mary/.test(str2) ); // false
```
The pattern `pattern:^Mary` means: "the string start and then Mary".
Now let's test whether the text ends with an email.
To match an email, we can use a regexp `pattern:[-.\w]+@([\w-]+\.)+[\w-]{2,20}`. It's not perfect, but mostly works.
To test whether the string ends with the email, let's add `pattern:$` to the pattern:
```js run
let reg = /[-.\w]+@([\w-]+\.)+[\w-]{2,20}$/g;
let str1 = 'My email is mail@site.com';
let str2 = 'Everywhere Mary went, the lamp was sure to go';
alert( reg.test(str1) ); // true
alert( reg.test(str2) ); // false
```
We can use both anchors together to check whether the string exactly follows the pattern. That's often used for validation.
For instance we want to check that `str` is exactly a color in the form `#` plus 6 hex digits. The pattern for the color is `pattern:#[0-9a-f]{6}`.
To check that the *whole string* exactly matches it, we add `pattern:^...$`:
```js run
let str = "#abcdef";
alert( /^#[0-9a-f]{6}$/i.test(str) ); // true
```
The regexp engine looks for the text start, then the color, and then immediately the text end. Just what we need.
```smart header="Anchors have zero length"
Anchors just like `\b` are tests. They have zero-width.
In other words, they do not match a character, but rather force the regexp engine to check the condition (text start/end).
```
The behavior of anchors changes if there's a flag `pattern:m` (multiline mode). We'll explore it in the next chapter.