diff --git a/Regular Expressions/Hexadecimal color/readme.md b/Regular Expressions/Hexadecimal color/readme.md new file mode 100644 index 0000000000..44c9fe9aed --- /dev/null +++ b/Regular Expressions/Hexadecimal color/readme.md @@ -0,0 +1,9 @@ +## This code snippet helps validating the hexadecimal color codes ## + +Based on the regular expression, the following formats are allowed: +* #ABC +* #AB1 +* #123 +* #ABCDEF +* #ABC123 +* #123456 diff --git a/Regular Expressions/Hexadecimal color/validateHexColor.js b/Regular Expressions/Hexadecimal color/validateHexColor.js new file mode 100644 index 0000000000..6c2a26c512 --- /dev/null +++ b/Regular Expressions/Hexadecimal color/validateHexColor.js @@ -0,0 +1,15 @@ +/** +* Hexadecimal color pattern. The following formats are allowed: +* #ABC | #AB1 | #123 +* #ABCDEF | #ABC123 | #123456 +*/ +const hexColorRegex = /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/g; + +const colorCode = "#ABC123"; + +if (hexColorRegex.test(colorCode)) { + gs.info("Valid hexadecimal color"); +} +else { + gs.info("Invalid hexadecimal color"); +}