# Regex for a hex colour

`/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/`

**Match a whole string consisting of "#", then (exactly 3 of "0" to "9", "a" to "f", or "A" to "F" or exactly 6 of "0" to "9", "a" to "f", or "A" to "F").**

## Step by step

- `^` — Anchor to the start of the input.
- `#` — Match "#".
- `(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})` — Match (exactly 3 of "0" to "9", "a" to "f", or "A" to "F" or exactly 6 of "0" to "9", "a" to "f", or "A" to "F").
- `$` — Anchor to the end of the input.

## Warnings

### Anchors match string ends, not line ends (info)

Without the m flag, ^ and $ match only the very start and end of the input — not the start and end of each line. On multi-line input this is often not what was intended.

**Fix:** Add the m flag if the pattern should apply per line.

## Details

- Capture groups: none

---

Canonical URL: https://regex-explainer.gumballtools.com/regex/regex-for-hex-color
JSON API: `GET https://regex-explainer.gumballtools.com/api/v1/explain?pattern=...`
MCP endpoint: `https://regex-explainer.gumballtools.com/api/mcp`
