Pattern library / Strings & Text
Regex to Match Short Hex Colors (#RGB)
Matches the shorthand #RGB form (each digit doubled to make #RRGGBB). Combine with the full hex color regex to accept both forms.
How the pattern works
#— literal hash.[0-9a-fA-F]{3}— exactly 3 hex digits.- To accept both 3- and 6-digit:
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
#f00 | Match | red shorthand | Try → |
#abc | Match | lowercase hex | Try → |
#ABC | Match | uppercase hex | Try → |
#ff0000 | No match | 6 digits, not 3 | Try → |
f00 | No match | missing # | Try → |
#f0 | No match | only 2 digits | Try → |
Where to use it
JavaScript
const re = /^#[0-9a-fA-F]{3}$/;
re.test('#f00'); // true
Python
import re
re.match(r'^#[0-9a-fA-F]{3}$', '#f00')
Common mistakes
Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
FAQ
Related patterns
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.