Regex library
Regular expression for hex colors
Match CSS hex colors in #RGB or #RRGGBB form. #FFFF and missing hashes are rejected.
^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Runs 100% in your browser. Nothing you paste is uploaded, stored, or sent to a server.
Highlighted matches
How to use
- Scan the pre-filled pattern and the sample. Hits are highlighted; misses stay plain.
- Toggle flags if you need global or case-insensitive matching. The pattern is JavaScript / ECMAScript.
- Paste your own corpus. Nothing is sent to a server.
- Read the notes below before you drop the pattern into production — several of these are intentionally incomplete.
Design tokens and theme files are full of #0f766e and #fff. This pattern accepts the two forms CSS has used for decades and rejects four- or five-digit lookalikes.
It does not accept #RRGGBBAA. If you are on a modern CSS stack that uses eight-digit hex, extend the second branch to {6}|{8}.
What this pattern will not do
The leading # is required.
Named colors such as teal are out of scope.
FAQ
Can I allow hex without a hash?
Make the # optional: ^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$. Require it in UI if you can.
What about rgb()?
Different grammar. Keep hex and functional notations in separate checks.
Is the sample text uploaded?
No. The tester compiles the pattern with the browser’s RegExp engine and stays on the device.