A gist for explaining the regex used specifically for hex codes.
A Regex, also known as a Regular Expression, is a sequence of characters that specifies a matching pattern in text. It is considered a literal, so the pattern will always be wrapped in slash characters (/
). In this gist we will be examining regexes and specifically the regex that matches hex values:
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
A hex value, or hex code, is a six-character code preceded by a #
. Sometimes a shorthand comprised of three charactes is used to represent the traditional six characters. In HTML it is used to represent colors. Using hex values is a written way of representing the amount of red, green, and blue in any particular shade of color, and it also visually represents the binary code of that specific color (since we aren't computers).