$devtoolkit.sh/examples/regex/hex-color

Regex Pattern for Hex Colors

Hex color codes appear in CSS, design tokens, and configuration files. This example matches both shorthand 3-digit (#RGB) and full 6-digit (#RRGGBB) formats with an optional leading hash. The tester helps you verify edge cases like uppercase letters and missing hashes. Extend the pattern to support 8-digit hex with alpha channel (#RRGGBBAA) if your use case requires it.

Example
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/

# Test cases:
#FF5733
#fff
FF5733
#AABBCC
#GG0000
#12345
#1234567
rgb(255,0,0)
[ open in Regex Tester → ]

FAQ

What is the difference between 3-digit and 6-digit hex colors?
A 3-digit color like #RGB is shorthand where each digit is doubled, so #ABC expands to #AABBCC. Both represent the same 256³ color space with less precision.
How do I match hex colors with alpha channel?
Add a third alternation for 8-digit hex: #?([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3}). The last two digits represent the alpha channel.
How do I extract all hex colors from a CSS file?
Use the global flag /g and a pattern with word boundaries to find all hex colors in a block of CSS text. The tester shows all matches highlighted in the input.

Related Examples

/examples/regex/hex-colorv1.0.0