Back to the tool

Regex for Hex Color Codes: the Pattern, the Shorthand Trap, and the Alpha Forms

The pattern for a hex color code is 24 characters long, and it still trips people up. Here it is:

^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

That accepts #fff and #ffffff, with or without the hash, upper or lower case. It is the answer to "regex for hex color code" in one line. The rest of this article is about the three ways that one line goes wrong in real code.

Reading the pattern left to right

Piece by piece. The ^ and $ anchors pin the match to the whole string. Without them, the pattern finds a hex code hiding inside any longer string, which is the most common bug in validation code. The #? makes the hash optional: CSS color values include it, but a lot of design-tool configs and JSON color fields store the bare digits. [A-Fa-f0-9] matches one hexadecimal digit, letters a through f in either case plus 0 through 9. The {6}|{3} alternation says either six digits or three.

The alternation-order trap

Now the first trap: alternation order. The {6} has to come before the {3}. Regex alternation tries the left option first, and with anchors in place the engine backtracks to try the other branch if the first fails, so anchored matching works either way. But the moment you drop the anchors to search inside text instead of validating, the order decides which match you get: {3} first matches the first three digits of a six-digit code and reports success, leaving the rest. Put the longer option first and this class of bug disappears. It costs nothing and it reads as intent.

The shorthand trap

The second trap is the shorthand itself. #fff is not a truncated #ffffff by accident. In CSS, each digit of a 3-digit code is doubled: #fff means #ffffff, #6d8 means #66dd88. The regex accepts the shorthand fine. Expanding it is the application's job, and that is where the failures happen: code that validates #abc and then passes 3 characters to a function expecting 6.

The alpha forms

The third trap is the alpha channel. Modern CSS added 4-digit (#RGBA) and 8-digit (#RRGGBBAA) forms for colors with transparency, like #ff0080cc. The classic pattern rejects them. If your input can carry transparency, extend the alternation:

^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{3})$

Longest first, same rule as before. Four branches now, which is where the pattern starts to look unwieldy, and that is a sign to stop adding branches and decide what your input actually allows. Most form fields only ever see 3 and 6. Design tools that export alpha need 4 and 8. Pick the set your data has, not every set that exists.

Case: explicit class or the flag

One more decision: case. [A-Fa-f0-9] accepts both cases explicitly. The alternative is [0-9a-f] with the case-insensitive flag, which is shorter and does the same thing. I prefer the explicit character class in shared code because the flag is invisible at the call site: someone reading the pattern sees exactly what is allowed. In your own code, use the flag. In a library or a template other people will copy, spell it out.

People argue about this last one. Do not write your own when your language has a parser. Every mainstream language can parse a hex color without regex, and the parser handles the edge cases, the alpha forms, the whitespace, that the pattern above ignores. Regex is the right tool when you are validating a form field or scanning text. It is the wrong tool when you already have the string and just need the color. Know which job you are doing.

Frequently asked questions

What is the regex for a hex color code?

^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ matches 3 or 6 hex digits with an optional leading hash, in either case. Add {8} and {4} branches for the CSS alpha forms, longest first.

Why does alternation order matter in the hex color regex?

Regex tries the left branch first. With anchors, backtracking covers you, but in unanchored searches {3} first matches only the first three digits of a six-digit code. Put {6} before {3} so the longer form wins.

What does #fff expand to?

#ffffff. Each digit of a 3-digit CSS hex code is doubled: #6d8 becomes #66dd88. Your validator accepts the shorthand, but downstream code must expand it before use.

How do I validate hex colors with transparency?

Use the 8-digit (#RRGGBBAA) and 4-digit (#RGBA) forms: ^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{4}|[A-Fa-f0-9]{3})$. Only include the branches your input actually allows.

Should the hash be required or optional in a hex color regex?

It depends on the source. CSS values include it; design tools and JSON configs often store bare digits. #? makes it optional. If your data always has one form, require that form and reject the other.

Related reading: Regex for Time in hh:mm · Why Your Date Validation Regex Fails · Regex for Postal Code Validation · Regex Word Boundaries, Explained · Email Regex: the Mistakes Everyone Makes

Try the tool

Start from a working template instead of a blank page. Customize a regex template with plain-English explanations of what each part does.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.