Given a string like #fff443
or #999999
How do I verify that the string has:
The length of the hexadecimal color code should be either 6 or 3, excluding '#' symbol. For example: #abc, #ABC, #000, #FFF, #000000, #FF0000, #00FF00, #0000FF, #FFFFFF are all valid Hexadecimal color codes.
Yes, in hexadecimal, things like A, B, C, D, E, and F are considered numbers, not letters. That means that 200 is a perfectly valid hexadecimal number just as much as 2FA is also a valid hex number.
regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"; Where: ^ represents the starting of the string. # represents the hexadecimal color code must start with a '#' symbol.
It seems that you are matching against a css color:
function isValidColor(str) {
return str.match(/^#[a-f0-9]{6}$/i) !== null;
}
To elaborate:
^
match beginning#
a hash[a-f0-9]
any letter from a-f and 0-9{6}
the previous group appears exactly 6 times$
match endi
ignore case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With