/^#[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.