Regexp Matching Hex Color Syntax (and Shorten form)
/^#[0-9a-f]{3,6}$/i
would match #abc
, #abcd
, #abcde
, #abcdef
/^#([0-9a-f]{3}|[0-9a-f]{6})$/i
would match #abc
and #abcdef
but not #abcd
/^#([0-9a-f]{3}){1,2}$/i
would match #abc
and #abcdef
but not #abcd
/^#(?:[0-9a-f]{3}){1,2}$/i
would match #abc
and #abcdef
but not #abcd
Have a look at RegExp - MDN to learn more about regular expressions in javascript.
Try this :
/^#([0-9a-f]{6}|[0-9a-f]{3})$/i
[0-9a-f]{6}
= 6 characters
[0-9a-f]{3}
= 3 characters
$
= end