Skip to the content

Hex Color Regex

A regular expression for CSS hex colors of three, four, six or eight digits, filled in with test lines and taken apart piece by piece.

Try:

JavaScript syntax, run by your browser's own RegExp engine.

5 matches.

Matches

  1. 1#fffindex 0
  2. 2#FFFindex 5
  3. 3#7c3aedindex 10
  4. 4#7C3AED80index 18
  5. 5#0f08index 28
Cheat sheet

Characters

.
Any character except a line break
\d \D
A digit 0-9, anything but a digit
\w \W
A letter a-z, a digit or an underscore; anything else
\s \S
White space, anything but white space
[abc] [^abc]
One of a, b, c; any character except those
[a-z0-9]
A character in one of the ranges
\p{L}
A letter in any script (needs the u flag)
\. \\ \/
A literal dot, backslash, slash

How many

a*
Zero or more
a+
One or more
a?
Zero or one
a{3}
Exactly three
a{2,5}
Two to five
a{2,}
Two or more
a*? a+? a??
The same, but as few as possible (lazy)

Positions

^
Start of the text, or of a line with the m flag
$
End of the text, or of a line with the m flag
\b
The edge of a word
\B
Anywhere that is not the edge of a word

Groups

(abc)
A group whose text is remembered
(?<name>abc)
The same, with a name
(?:abc)
A group that is not remembered
a|b
Either a or b
\1 \k<name>
The same text a group matched earlier

Looking around

a(?=b)
a, only when b follows
a(?!b)
a, only when b does not follow
(?<=b)a
a, only when b comes before it
(?<!b)a
a, only when b does not come before it

In the replacement

$1 $2
The text of group 1, group 2
$<name>
The text of a named group
$&
The whole match
$` $'
The text before the match, after the match
$$
A dollar sign
Share this tool:TwitterFacebookLinkedInWhatsAppEmail

The pattern, piece by piece

For colors in CSS hex notation, one to a line.

/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/gim
^The start of the line.
#The hash sign that every hex color begins with.
(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})Three or four hex digits, or six, or eight, and nothing in between. The i flag lets A to F through as well as a to f.
$The end of the line.

The lengths that exist

CSS has a short and a long form, each with or without transparency:

[0-9a-f]{3,4}#rgb and #rgba. One digit per channel, each read as if it were doubled: #f80 is #ff8800.
[0-9a-f]{6}#rrggbb. Two digits each for red, green and blue.
[0-9a-f]{8}#rrggbbaa. The last two digits are the opacity.

What it gets right

These are the first lines in the box above. With the g and m flags every line is tested on its own, from its first character to its last.

#fffMatch
#FFFMatch
#7c3aedMatch
#7C3AED80Match
#0f08Match
#ffNo matchTwo digits.
#fffffNo matchFive digits: neither the short form nor the long one.
#1234567No matchSeven digits.
#gggNo matchg is not a hex digit.
7c3aedNo matchNo hash sign.

Where it goes wrong

The last lines in the box are there to show the limits: text the pattern refuses although it is fine, or lets through although it is not.

rebeccapurpleNo matchA color as well, by its name.
rgb(124 58 237)No matchThe same color as #7c3aed, written as red, green and blue.
transparentNo matchA keyword that CSS accepts wherever a color goes.

Before you rely on it

The forms with four and eight digits carry the opacity. They came with CSS Color Level 4, and not all older software accepts them. For the classic forms only, use ^#(?:[0-9a-f]{3}|[0-9a-f]{6})$.

A pattern cannot tell what a color looks like, or that two notations mean the same color. The color converter translates between them.

Finding colors in a stylesheet

Without the anchors and with a word boundary at the end, the pattern picks hex colors out of running CSS. It cannot tell a color from an id selector that happens to be made of hex letters, such as #facade.

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

Run on this text:

a { color: #7c3aed; background: #FFF8; border: 1px solid #e4e4e7; }
#header, #facade { margin: 0 }
/* too short or too long: #12, #1234567 */

it finds #7c3aed, #FFF8, #e4e4e7 and #facade, and nothing else.

The regex tester page explains the flags, has a cheat sheet of the syntax, and tells what happens when a pattern takes too long.

See all 12