//g
  • 100% Free
  • No Sign-Up
  • No Credit Card
  • No Free Trial
  • No Pro Tier
  • No Watermarks

Nothing is uploaded. Just open it and start.

Hex Colour Regex

A CSS hex colour: # followed by 3, 4, 6 or 8 hex digits (the 4- and 8-digit forms include transparency).

/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/

Piece by piece

  1. ^the start of the text
  2. #the character “#”
  3. (?:a group (not captured)
  4. [0-9a-fA-F]{3,4}one of 0–9, a–f, A–F, repeated between 3 and 4 times, as many as possible
  5. |or
  6. [0-9a-fA-F]{6}one of 0–9, a–f, A–F, repeated exactly 6 times, as many as possible
  7. |or
  8. [0-9a-fA-F]{8}one of 0–9, a–f, A–F, repeated exactly 8 times, as many as possible
  9. )end of the group
  10. $the end of the text

Examples

TextResult
#fffMatches
#1E88E5Matches
#1e88e580Matches
#abcdMatches
fffDoesn't match
#gggDoesn't match
#12345Doesn't match
#1234567Doesn't match
  • It doesn't accept colour names or rgb() values.

Other common patterns

Test any regular expression →

Frequently asked questions

How do I use this hex colour regex in JavaScript?
Write it as /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/ and call re.test(text). It is anchored with ^ and $, so it checks the whole text; remove them to find it inside longer text (and add the g flag to find every one). The Code view writes it out for Python, Go and PHP too.
Is it a complete validator?
No regex is the whole story. It doesn't accept colour names or rgb() values.
Can I try it on my own text?
Yes. The tester above already holds the examples; paste your own text over them and every match is highlighted as you type. Nothing is uploaded.

Spotted a bug or have a suggestion?

Found something broken, have an idea, or just want to say thanks about any of our tools? Every message reaches a real person.