HTML/CSS color are defined using six or three character hex values prefixed with an optional #
symbol
/^#?([a-f0-9]{6}|[a-f0-9]{3})$/i
This expression matches three or six valid hex digits prefixed with an optional #
- Anchors
- Quantifiers
- Grouping Constructs
- Bracket Expressions
- Character Classes
- The OR Operator
- Flags
- Character Escapes
A ^
symbol indicates start of string and a $
symbol marks the end.
Three quantifiers are used in this expression.
?
after the literal#
indicates zero or one of#
{6}
requires six characters from the[a-f0f-9]
class{3}
requires six characters from the[a-f0f-9]
class
()
characters designate a group. In this case the group is either 6 or 3 hex digits
[a-f0f-9]
matches characters a
through f
and 0
through 9
[a-f0f-9]
matches characters a
through f
and 0
through 9
The |
symbol allows the hex string be either of the patterns
The i
flag at the end makes the regex case insensitive
Not used