splitforms.com

Free tool - runs in your browser

Hex color regex tester

Test hex colors (#RGB, #RRGGBB, #RGBA and #RRGGBBAA, with or without the #, any case) and CSS rgb(), rgba() and hsl() values in comma or space syntax. Pick a preset, edit the regex and paste colors one per line: every match shows a swatch. Copy JavaScript, Python or an HTML pattern when you are ready.

3, 4, 6 or 8 hex digits, any case. The 4th or the last 2 digits are alpha: #ff000080 is red at about 50% opacity.

5 of 11 match

  • Match#fff
  • Match#FFFFFF
  • Match#ff000080
  • Match#f008
  • Match#00ff00FF
  • No match#ggg
  • No match#12345
  • No match#1234567
  • No match#fffffffff
  • No matchff000080
  • No match#ff 00 00

HTML pattern uses the v flag and has no i flag, so the HTML version lists both a-f and A-F. Validate again on the server; Python snippets use ASCII matching to keep digit ranges consistent with JavaScript.

Hex, rgb() and hsl() color patterns

Hex presets use the i flag, so upper and lower case both match. The rgb() presets limit each channel to 0-255, and the hsl() presets limit hue to 0-360 and saturation and lightness to 0%-100%. Alpha may be 0 to 1 or 0% to 100%. Default hex pattern: ^#([0-9a-f]{3}|[0-9a-f]{6})$. Every preset below is checked against its match and no-match samples.

PresetRegexMatch / no match examplesNotes
Hex #RGB or #RRGGBB/^#([0-9a-f]{3}|[0-9a-f]{6})$/iMatch: #fff, #FFF, #ff4f00, #FFFFFF, #0a0B0c
No match: #ff000080, #ggg, #12345, #ffff, fff, #fffffff, # fff; empty string
A # then exactly 3 or 6 hex digits, any case. No alpha channel. This is the classic CSS hex color.
Hex with alpha (#RGB, #RGBA, #RRGGBB, #RRGGBBAA)/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/iMatch: #fff, #FFFFFF, #ff000080, #f008, #00ff00FF
No match: #ggg, #12345, #1234567, #fffffffff, ff000080, #ff 00 00; empty string
3, 4, 6 or 8 hex digits, any case. The 4th or the last 2 digits are alpha: #ff000080 is red at about 50% opacity.
Hex with optional #/^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/iMatch: #fff, fff, FF4F00, #ff000080, ff000080
No match: ##fff, #ggg, #12345, 12345, 0xff4f00; empty string
Same lengths as the alpha preset, but the # is optional. Add the # back before you use the value in CSS.
rgb() / rgba() comma syntax^rgba?\(\s*(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*,\s*(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*,\s*(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*(?:,\s*(?:0|1|0?\.\d+|1\.0+|(?:100|[1-9]?\d)%)\s*)?\)$Match: rgb(255,0,0), rgb(255, 79, 0), rgba(0, 0, 0, 0.5), rgba(255,255,255,.25), rgb(0, 128, 255, 50%)
No match: rgb(256,0,0), rgb(255,0), rgb(-1,0,0), rgb(01,0,0), rgba(0,0,0,1.5), rgb(255 0 0), RGB(255,0,0); empty string
Three channels 0-255 (no leading zeros), commas, and an optional alpha from 0 to 1 or 0% to 100%. rgb() and rgba() are aliases in modern CSS. Function names are lowercase.
rgb() modern space syntax^rgba?\(\s*(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s+(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s+(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\s*(?:/\s*(?:0|1|0?\.\d+|1\.0+|(?:100|[1-9]?\d)%)\s*)?\)$Match: rgb(255 0 0), rgb(255 0 0 / 50%), rgb(0 128 255 / 0.5), rgba(12 34 56 / .8)
No match: rgb(256 0 0), rgb(255,0,0), rgb(255 0 0 / 150%), rgb(255 0 0 50%), rgb(255 0); empty string
Space-separated 0-255 channels with an optional alpha after a slash, as in CSS Color 4. Percent channels and the none keyword are not covered.
hsl() / hsla() comma syntax^hsla?\(\s*(360|3[0-5]\d|[12]\d\d|[1-9]?\d)(?:deg)?\s*,\s*(100|[1-9]?\d)%\s*,\s*(100|[1-9]?\d)%\s*(?:,\s*(?:0|1|0?\.\d+|1\.0+|(?:100|[1-9]?\d)%)\s*)?\)$Match: hsl(0, 100%, 50%), hsl(360,0%,0%), hsla(120, 60%, 70%, 0.3), hsl(210deg, 40%, 96%)
No match: hsl(361, 100%, 50%), hsl(0, 101%, 50%), hsl(0, 100, 50), hsl(0 100% 50%), hsla(0, 0%, 0%, 2); empty string
Hue 0-360 (whole degrees, optional deg unit), saturation and lightness 0%-100% with the % sign, and an optional alpha.
hsl() modern space syntax^hsla?\(\s*(360|3[0-5]\d|[12]\d\d|[1-9]?\d)(?:deg)?\s+(100|[1-9]?\d)%\s+(100|[1-9]?\d)%\s*(?:/\s*(?:0|1|0?\.\d+|1\.0+|(?:100|[1-9]?\d)%)\s*)?\)$Match: hsl(0 100% 50%), hsl(210 40% 96% / 0.5), hsl(120deg 60% 70% / 30%)
No match: hsl(400 100% 50%), hsl(0 100% 101%), hsl(0, 100%, 50%), hsl(0 100% 50% 0.5); empty string
Space-separated hue, saturation and lightness with an optional alpha after a slash. Decimal hues, turn/rad units and none are not covered.

Hex color pattern in an HTML form

color-form.html

<form action="https://splitforms.com/api/submit" method="POST">
  <input type="hidden" name="access_key" value="YOUR_KEY">

  <label for="brand-color">Brand color (hex)</label>
  <input id="brand-color" name="brand_color" type="text" pattern="#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})" title="Hex color such as #ff4f00 or #fff" placeholder="#ff4f00" required>

  <label for="accent-color">Accent color</label>
  <input id="accent-color" name="accent_color" type="color" value="#ff4f00">

  <button type="submit">Send</button>
</form>

A text input can enforce a hex format with the pattern attribute. HTML anchors the whole value, uses the v flag and has no case-insensitive flag, so the pattern lists both a-f and A-F. Add required to reject an empty value. The form posts to splitforms, so each submitted color arrives in your inbox; check it again on your server before you save it.

A native input type=color ignores pattern and always gives a lowercase 7-character #rrggbb value, such as #ff4f00. It never sends #fff, uppercase letters, alpha, rgb() or hsl(). Use a text input with one of these presets when people need to type or paste other color formats.

Hex color regex FAQ

What is the regex for a hex color code?

Use ^#([0-9a-f]{3}|[0-9a-f]{6})$ with the i flag. It accepts # followed by exactly 3 or 6 hex digits in any case, such as #fff, #FFF and #ff4f00, and rejects #ggg, #12345 and values without the #. In an HTML pattern attribute there is no i flag, so write [0-9a-fA-F] instead.

How do I allow 8-digit hex colors with alpha?

Use ^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$ with the i flag. CSS reads the 4th digit of #RGBA or the last two digits of #RRGGBBAA as opacity, so #ff000080 is red at about 50% opacity. Lengths 5 and 7 are never valid hex colors.

How do I check that rgb() values are between 0 and 255?

Regex cannot compare numbers, so each channel uses the alternation (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d). It accepts 0-255 without leading zeros, so rgb(256,0,0) and rgb(-1,0,0) fail. The same idea limits hsl() hue to 0-360 and saturation and lightness to 0%-100%.

Does the regex support the modern rgb(255 0 0 / 50%) syntax?

Yes, with the space syntax presets. CSS Color 4 lets rgb() and hsl() use spaces between channels and a slash before alpha. Comma and space syntax cannot be mixed, so rgb(255 0 0) fails the comma preset and rgb(255,0,0) fails the space preset. Combine both regexes with | if you want to accept either.

What does input type=color send to the server?

A native color input always submits a lowercase 7-character #rrggbb value, such as #ff4f00. It never sends short #fff, uppercase letters, alpha, rgb() or hsl(), and it is never empty. The pattern attribute does not apply to type=color, so use a text input with pattern when people need to type other formats.

More free form tools

Form backend cost calculator

Enter monthly submissions and compare the cheapest listed plan across eight form backends.

Email deliverability checker

Enter a domain to check its SPF, DMARC and MX records and get a plain-English fix for each.

SPF record checker

Look up a domain's SPF record, see each mechanism explained and count DNS lookups against the 10-lookup limit.

DMARC record generator

Pick a policy and report address and copy a ready _dmarc TXT record.

DMARC checker

Look up a domain's DMARC record and see the policy and every tag explained.

DKIM record checker

Enter a domain and selector to check its DKIM key, with presets for common senders.

SPF record generator

Tick your email providers and copy one SPF TXT record with a lookup count.

Contact form spam checker

Paste form HTML to audit its honeypot, endpoint, CAPTCHA markers and spam resistance.

Honeypot field generator

Generate a hidden anti-spam input with your chosen name and hiding technique.

Form action URL tester

Check a form action URL for reachability, CORS headers, POST acceptance and backend.

Email obfuscator

Encode your email so spam scrapers can't read it but people can still click it.

Mailto link generator

Build a mailto: URL with recipients, CC, BCC, subject and a pre-filled body.

HMAC-SHA256 generator

Generate and verify webhook signatures for common providers in your browser.

URL encoder and decoder

Encode text, build an x-www-form-urlencoded body or parse a query string.

UTM builder

Build campaign URLs with UTM parameters, tag a CSV in bulk and check tagged links.

HTML to JSX converter

Paste HTML and get JSX or a working React/Next.js form component.

Image to Base64 converter

Convert images to Base64 data URLs in your browser and copy HTML and CSS snippets.

Base64 to image converter

Paste a Base64 data URI to preview and download the decoded image.

Base64 decode & encode

Decode Base64 to readable UTF-8 text or encode text to Base64, locally in your browser.

URL to Base64 converter

Fetch a direct image URL in your browser and get its Base64 data URL.

AI form generator

Describe your form in plain English and copy working HTML.

See all free tools