Web
HTML5 Color Input Value
Validate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^#[0-9a-fA-F]{6}$", "");
const input = "#ff5733";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^#[0-9a-fA-F]{6}$")
input_text = "#ff5733"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
input := `#ff5733`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
^#[0-9a-fA-F]{6}$Raw source: ^#[0-9a-fA-F]{6}$
How it works
^# requires the leading hash. [0-9a-fA-F]{6} requires exactly 6 hexadecimal digits. $ anchors the end. Note this is STRICTER than the general hex-color pattern: the HTML5 color input only accepts the 6-digit form, not the 3-digit shorthand.
Examples
Input
#ff5733Matches
#ff5733
Input
#0F0F0FMatches
#0F0F0F
Input
#fffNo match
—Common use cases
- •Form validation before submitting a color value to a backend
- •Storing color picker output safely
- •Migrating from looser to stricter color formats
- •Round-tripping HTML5 input values in design tools
Related patterns
Cookie Header Value
WebParse name=value pairs from an HTTP `Cookie:` header value.
Hex Color Code
WebMatch CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.
CSS hsl() / hsla() Color
WebMatch CSS hsl() and hsla() color functions, capturing hue (0–360), saturation, lightness, and optional alpha.
CSS rgb() Color
WebMatch CSS rgb() and rgba() color functions, including an optional alpha channel.