Skip to main content
Regexflux

Hex Color Code

beginner

Matches CSS hex color codes in 3, 4, 6, or 8 digit formats.

colorhexcssdesign

Pattern

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

Try It

0 of 8 strings matched

Explanation

Matches # followed by 3 digits (short RGB), 4 digits (short RGBA), 6 digits (full RGB), or 8 digits (full RGBA) of hexadecimal.

Test Strings

Matching

  • #fff
  • #FF5733
  • #00ff0080
  • #abcd

Non-matching

  • #gg0000
  • #12
  • #1234567890
  • FF5733

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOLimited support (ASCII-only word boundaries)

Code Snippets

const regex = /#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Related Patterns