File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed
9-regular-expressions/09-regexp-quantifiers/2-find-html-colors-6hex Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change 1- We need to look for ` # ` followed by 6 hexadecimal characters .
1+ Нам потрбіно знайти символ ` # ` за яким слідують 6 шістнадцяткових символів .
22
3- A hexadecimal character can be described as ` pattern:[0-9a-fA-F] ` . Or if we use the ` pattern:i ` flag, then just ` pattern:[0-9a-f] ` .
3+ Шістнадцятковий символ можна описати як ` pattern:[0-9a-fA-F] ` . Або, якщо застосувати прапорець ` pattern:i ` , то запис скоротиться до ` pattern:[0-9a-f] ` .
44
5- Then we can look for 6 of them using the quantifier ` pattern:{6} ` .
5+ Далі ми позначимо за допомогою квантифікатора, що нам потрібно саме 6 таких шістнадцяткових символів ` pattern:{6} ` .
66
7- As a result, we have the regexp : ` pattern:/#[a-f0-9]{6}/gi ` .
7+ І у результаті, отримаємо такий регулярний вираз : ` pattern:/#[a-f0-9]{6}/gi ` .
88
99``` js run
1010let regexp = / #[a-f0-9 ] {6} / gi ;
@@ -14,18 +14,17 @@ let str = "color:#121212; background-color:#AA00ef bad-colors:f#fddee #fd2"
1414alert ( str .match (regexp) ); // #121212,#AA00ef
1515```
1616
17- The problem is that it finds the color in longer sequences:
18-
17+ Проблема в тому, що вищевказаний регулярний вираз знаходитиме код кольору навіть у довших послідовностях.
1918``` js run
2019alert ( " #12345678" .match ( / #[a-f0-9 ] {6} / gi ) ) // #123456
2120```
2221
23- To fix that, we can add ` pattern:\b ` to the end :
22+ Щоб виправити це, ми додамо ` pattern:\b ` на кінці виразу :
2423
2524``` js run
26- // color
25+ // колір
2726alert ( " #123456" .match ( / #[a-f0-9 ] {6} \b / gi ) ); // #123456
2827
29- // not a color
28+ // не колір
3029alert ( " #12345678" .match ( / #[a-f0-9 ] {6} \b / gi ) ); // null
3130```
You can’t perform that action at this time.
0 commit comments