File tree Expand file tree Collapse file tree 2 files changed +5
-5
lines changed
5-regular-expressions/09-regexp-groups/1-find-webcolor-3-or-6 Expand file tree Collapse file tree 2 files changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ The simplest way to add them -- is to append to the regexp: `pattern:/#[a-f0-9]{
6
6
7
7
We can do it in a smarter way though: ` pattern:/#([a-f0-9]{3}){1,2}/i ` .
8
8
9
- Here the regexp ` pattern:[a-f0-9]{3} ` is in parentheses to apply the quantifier ` pattern:{1,2} ` to it as a whole.
9
+ Here the regexp ` pattern:[a-f0-9]{3} ` is in parentheses to apply the quantifier ` pattern:{1,2} ` to it as a whole.
10
10
11
11
In action:
12
12
@@ -15,15 +15,15 @@ let reg = /#([a-f0-9]{3}){1,2}/gi;
15
15
16
16
let str = " color: #3f3; background-color: #AA00ef; and: #abcd" ;
17
17
18
- alert ( str .match (reg) ); // #3f3 #AA0ef #abc
18
+ alert ( str .match (reg) ); // #3f3 #AA00ef #abc
19
19
```
20
20
21
- There's minor problem here: the pattern found ` match:#abc ` in ` subject:#abcd ` . To prevent that we can add ` pattern:\b ` to the end:
21
+ There's a minor problem here: the pattern found ` match:#abc ` in ` subject:#abcd ` . To prevent that we can add ` pattern:\b ` to the end:
22
22
23
23
``` js run
24
24
let reg = / #([a-f0-9 ] {3} ){1,2} \b / gi ;
25
25
26
26
let str = " color: #3f3; background-color: #AA00ef; and: #abcd" ;
27
27
28
- alert ( str .match (reg) ); // #3f3 #AA0ef
28
+ alert ( str .match (reg) ); // #3f3 #AA00ef
29
29
```
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ let reg = /your regexp/g;
8
8
9
9
let str = " color: #3f3; background-color: #AA00ef; and: #abcd" ;
10
10
11
- alert ( str .match (reg) ); // #3f3 #AA0ef
11
+ alert ( str .match (reg) ); // #3f3 #AA00ef
12
12
```
13
13
14
14
P.S. This should be exactly 3 or 6 hex digits: values like ` #abcd ` should not match.
You can’t perform that action at this time.
0 commit comments