Advertisement

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.

The pattern
^#[0-9a-fA-F]{3}$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
#f00Matchred shorthandTry →
#abcMatchlowercase hexTry →
#ABCMatchuppercase hexTry →
#ff0000No match6 digits, not 3Try →
f00No matchmissing #Try →
#f0No matchonly 2 digitsTry →

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.
Open the regex tester →

FAQ

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.