Hex Color Code Regex

Validate 3-character and 6-character hexadecimal CSS color codes with leading hash symbol.

//g
Valid: #FFF, #ff5733, #000000, #38BDF8 Invalid: #FFFF, #12345, FFFFFF

Regex Cheatsheet

COMMON PATTERNS
SYNTAX REFERENCE
TOOLS FOR WRITING BETTER CODE
JetBrains All Products Pack
IDEs with built-in regex debugging, refactoring, and inspections.
Try free โ†’
Regex course on Udemy
Go from this cheatsheet to writing complex patterns confidently.
View course โ†’
DigitalOcean / hosting credit
Deploy the scripts you are testing patterns for.
Get credit โ†’
Affiliate disclosure: links above may earn us a commission โ€” this never affects the calculation results above.

Common Use Cases for Hex Color Code Regex

Theme & Style Customizers

Validate user-selected custom brand and theme color input strings.

Frequently Asked Questions

Does it support 8-digit hex with alpha transparency?

You can extend the pattern to ^#([0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$ for alpha channel support.