en.javascript.info/10-regular-expressions-javascript/10-regexp-ahchors/2-test-mac/solution.md
2015-04-07 15:22:06 +03:00

21 lines
985 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Двузначное шестнадцатиричное число -- это ``pattern`[0-9a-f]{2}` (с учётом флага ``pattern`/i`).
Нам нужно одно такое число, и за ним ещё 5 с двоеточиями перед ними: ``pattern`[0-9a-f]{2}(:[0-9a-f]{2}){5}`
И, наконец, совпадение должно начинаться в начале строки и заканчиваться -- в конце. То есть, строка целиком должна подходить под шаблон. Для этого обернём шаблон в ``pattern`^...$`.
Итого, в действии:
```js
//+ run
var re = /^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/i;
alert( re.test('01:32:54:67:89:AB') ); // true
alert( re.test('0132546789AB') ); // false (нет двоеточий)
alert( re.test('01:32:54:67:89') ); // false (5 чисел, а не 6)
alert( re.test('01:32:54:67:89:ZZ') ) // false (ZZ в конце)
```