Stylesheets, theme files and design tokens collect hex colours by the hundred, and sooner or later you need all of them at once: to audit a palette, to find a stray colour that should have been a variable, or to check what a contractor actually shipped. Reading them out by eye is slow and unreliable. A regular expression can list every hex colour in a file in one pass, if the pattern is built to respect the four shapes a hex colour can take.

This guide builds that pattern piece by piece with the free Regex Tester on Ovatly: the digit class, the allowed lengths, and the small trailing detail that stops the pattern from clipping longer codes. The tester runs a JavaScript regular expression against your text live and lists every match with its position and capture groups, so each version of the pattern is proven against real text before you trust it.

Contents

  • Quick answer
  • The four shapes of a hex colour
  • Building the pattern piece by piece
  • How to match hex colour codes, step by step
  • Worked example: auditing a line of CSS
  • Limitations and alternatives
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Open the Regex Tester, type #(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b in the Pattern field without slashes, put g in the Flags field, and paste your stylesheet or config text. The match list shows every hex colour with its index. The pattern accepts the 3, 4, 6 and 8 digit forms, in upper or lower case, and rejects wrong lengths like #12 and #12345. Remember what it proves: shape only, never whether a colour is actually used.

The four shapes of a hex colour

A hex colour is a # sign followed by hexadecimal digits, and the digits come in fixed counts. The 6 digit form, #RRGGBB, is the everyday one: two digits each for red, green and blue. The 3 digit form, #RGB, is shorthand: each digit is doubled, so #fff is the same colour as #ffffff. The 4 and 8 digit forms add an alpha channel, which sets opacity: #RGBA is the shorthand with alpha, and #RRGGBBAA is the full form with alpha. No other count is a colour. Two digits, five digits or seven digits after the # is something else, a broken value or a different kind of token entirely, and a good pattern should refuse all of them.

Building the pattern piece by piece

Start with the digit class. Hexadecimal digits are 0 to 9 and a to f, in either case, which gives the class [0-9a-fA-F]. One length at a time, the pattern is simple: #[0-9a-fA-F]{6} against the text color: #1a2b3c; reports #1a2b3c at index 7. Correct, and useless on its own, because real files mix all four forms.

The obvious shortcut, accepting just the shortest form, has a trap. #[0-9a-fA-F]{3} against the same text reports #1a2 at index 7. The pattern found three hex digits after a # and stopped, clipping a 6 digit colour into a fake 3 digit one. Any single-length version has the mirror-image problem: it misses or mangles every other form.

The full pattern offers all four lengths as alternatives inside a non-capturing group, and ends with \b, a word boundary:

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

The trailing \b is what makes the short alternatives safe. Against #1a2b3c, the 3 digit branch matches 1a2, but then the next character is another hex digit, which is a word character, so there is no boundary and the branch fails. The engine tries the next alternatives until the 6 digit branch matches the whole code, with the boundary satisfied by the ; after it. The same boundary rejects #12345: every branch either runs out of digits or is followed by another digit, so nothing matches.

How to match hex colour codes, step by step

  1. Open the Regex Tester.
  2. Type the full pattern in the Pattern field, without slashes: #(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b
  3. Put g in the Flags field. Without it, only the first match is reported, which defeats the purpose of an audit.
  4. Paste your CSS, theme file or config text.
  5. Read the match list: each match is one colour, and its index tells you where in the file it sits.
  6. Sanity-check the rejects. Include a known bad value like #12345 in a test copy and confirm it produces no match.

Worked example: auditing a line of CSS

Take this test text, a single line with good colours and deliberate junk mixed in:

a{color:#fff;background:#1A2B3C;border:#abcd;shadow:#1a2b3c80;bad:#12;no:#gggggg;five:#12345}

Run the full pattern with flag g. The tester reports exactly four matches: #fff at index 8, #1A2B3C at index 24, #abcd at index 39 and #1a2b3c80 at index 52. That is one of each valid form: the 3 digit shorthand, a 6 digit colour in uppercase, the 4 digit alpha shorthand, and the 8 digit form with alpha. The three bad values all fail, each for a provable reason: #12 has too few digits for any branch, #gggggg contains no hex digits at all, and #12345 has five digits, a count no branch accepts and the trailing boundary will not let a shorter branch fake. Run the same test without the g flag and only #fff at index 8 is reported, which is the tester behaving as documented, and a reminder that the flag is part of the pattern.

Limitations and alternatives

Be clear about what a match means. The pattern checks shape only: a # sign plus the right count of hex digits. It cannot tell you whether a colour is valid in a design, whether it is ever applied to an element, or whether it sits in a comment or a dead variable. It will also match hex-shaped strings that are not colours at all, such as a fragment identifier in a URL or a short hash, if they happen to have 3, 4, 6 or 8 hex digits after a #. Treat the match list as candidates to review, not verdicts. The tester uses JavaScript's RegExp engine, whose syntax differs slightly from PCRE, Python or grep flavours, and results stop at 500 matches, so for a very large codebase, prove the pattern here and then run it in your editor or a script for the full sweep.

Common mistakes

  • Using one length only. #[0-9a-fA-F]{6} misses the shorthand forms, and #[0-9a-fA-F]{3} clips longer codes into fake matches.
  • Forgetting the trailing boundary. Without \b, the short alternatives succeed on the opening digits of longer codes, and your audit fills with fragments.
  • Leaving out a case. Real files mix #FFF and #fff. Cover both in the class, or use [0-9a-f] with the i flag.
  • Forgetting the g flag. You get the first colour and conclude the file is clean. It is not; you just stopped looking.
  • Using a capturing group out of habit. (...) works, but (?:...) avoids filling the group list with copies of every match when you only need the matches themselves.

FAQs

What lengths can a hex colour code be? Four lengths are in use: 3 digits (#RGB), 6 digits (#RRGGBB), and the alpha forms with 4 digits (#RGBA) and 8 digits (#RRGGBBAA), where the extra digits set the opacity. Anything with 2, 5 or 7 digits after the # is not a valid hex colour shape.

Why does #[0-9a-fA-F]{3} match part of a longer code? Because the pattern fits in the first three digits and the engine stops there. On #1a2b3c it matches #1a2. The fix is to offer the longer alternatives and end the pattern with \b, so a short branch fails when another hex digit follows and the engine tries the longer ones.

Do hex digits have to be lowercase? No. Hex digits run 0 to 9 and a to f, and both cases are used in real files. The class [0-9a-fA-F] covers both. You could also use [0-9a-f] with the i flag in the Flags field for the same effect.

Does a match prove the colour is really used in my design? No. A match proves only that a string has the shape of a hex colour: a # followed by the right count of hex digits. The code could sit in a comment, an unused variable or a string that was never applied. Checking shape is a fast way to find candidates, not proof of use.

Is my test text uploaded anywhere? No. The Regex Tester runs in your browser on your device, so the patterns and text you test with stay local.

Conclusion

A hex colour pattern earns its keep in the details: all four lengths offered, both cases covered, and a trailing boundary so nothing gets clipped. Build it once, prove it on text with known bad values mixed in, and the match list becomes a trustworthy audit of any file you paste. If your problem is the opposite one, a pattern that grabs far more than these tidy little codes, Why Does My Regex Match Too Much? Fixing Over-Matching works through the causes and fixes, and the rest of the Developer Tools guides cover more tools in this category.