Hex Color Code Regex

Example:
^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ Matches: ✓ #FF5733 ✓ #abc ✓ FF5733 (without #)
Validates hex colors in both 3-digit and 6-digit formats: ^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Try Regex Lab →

FAQs

With or without # prefix?

^#? makes the # optional, so both #FF5733 and FF5733 are valid.

What is the 3-digit format?

Shorthand like #abc expands to #aabbcc. Both are valid CSS.