Regex for hex color

/#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})\b/gi

Options
Matches

Examples

Note

Accepts 3, 4, 6 and 8 digits: the short and long forms, with and without alpha.

Matches in the example

  • "#fff"
  • "#1e90ff"
  • "#0008"
  • "#11223344"

Token by token

  1. # the character “#”
  2. (?: a group, not capturing
  3. [0-9a-f]{3,4} one of: “0” to “9” or “a” to “f”, between 3 and 4 times, as many as possible
  4. | or
  5. [0-9a-f]{6} one of: “0” to “9” or “a” to “f”, exactly 6 times
  6. | or
  7. [0-9a-f]{8} one of: “0” to “9” or “a” to “f”, exactly 8 times
  8. ) end of group
  9. \b a word boundary

Other patterns