Skip to content

Commit dd29cc2

Browse files
authored
Regular expression for validating hexadecimal color codes (#1504)
* Create validateHexColor.js * Create readme.md * Update validateHexColor.js
1 parent 44deb4e commit dd29cc2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## This code snippet helps validating the hexadecimal color codes ##
2+
3+
Based on the regular expression, the following formats are allowed:
4+
* #ABC
5+
* #AB1
6+
* #123
7+
* #ABCDEF
8+
* #ABC123
9+
* #123456
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Hexadecimal color pattern. The following formats are allowed:
3+
* #ABC | #AB1 | #123
4+
* #ABCDEF | #ABC123 | #123456
5+
*/
6+
const hexColorRegex = /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/g;
7+
8+
const colorCode = "#ABC123";
9+
10+
if (hexColorRegex.test(colorCode)) {
11+
gs.info("Valid hexadecimal color");
12+
}
13+
else {
14+
gs.info("Invalid hexadecimal color");
15+
}

0 commit comments

Comments
 (0)