Hex color regex pattern

Copy and test a JavaScript regex for hex color values, with examples and a one-click link to the regex tester.

Hex color regex pattern

Validate whole value

Use anchors to check whether the complete value matches.

Regex: ^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$

Flags: i

Search in text

Find matching fragments inside a larger block of text.

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

Flags: gi

Examples that match

  • #ABC
  • #1a2b3c

Examples that do not match

  • 123456
  • #12