Code
CSS hex color code Regex
CSS accepts hex colors in four lengths: three or six digits for plain RGB, and four or eight digits when an alpha channel is included. This pattern accepts exactly those lengths after a leading # sign, in upper or lower case. It is a good fit for theme editors, design token files, colour picker inputs and linters that check style sheets. Five and seven digit values are rejected because browsers ignore them. The pattern only checks the notation; it does not handle named colours like rebeccapurple or functional forms such as rgb() and hsl(), which need their own rules.
Pattern (JavaScript)
/^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$/Either: (1) Starts at the start of the line/string, then the character "#", then (one of the characters \da-fA-F, exactly 3 times), between 1 and 2 times, then the end of the line/string; or (2) Starts at the start of the line/string, then the character "#", then (one of the characters \da-fA-F, exactly 4 times), between 1 and 2 times, then the end of the line/string.
Examples
Matches
- ✓ #fff
- ✓ #1a2B3c
- ✓ #11223380
- ✓ #abcd
Does not match
- ✗ #ggg
- ✗ 123456
- ✗ #12345
- ✗ #1234567
Try it
0 matches (JavaScript engine)
#fff
#1a2B3c
#11223380
#abcd
#ggg
123456
#12345
#1234567Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$/g;
const text = "#fff";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$')
text = '#fff'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$/';
$text = '#fff';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^#(?:[\\da-fA-F]{3}){1,2}$|^#(?:[\\da-fA-F]{4}){1,2}$");
String text = "#fff";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$`)
text := "#fff"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$");
var text = "#fff";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$").unwrap();
let text = "#fff";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^#(?:[\da-fA-F]{3}){1,2}$|^#(?:[\da-fA-F]{4}){1,2}$/
text = '#fff'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- What do the 4 and 8 digit forms mean?
- They add an alpha channel to the colour. #RGBA and #RRGGBBAA describe transparency, where the last one or two digits set the opacity. All modern browsers support them.
- Why is the pattern split into two alternatives?
- One branch accepts one or two groups of three digits, giving 3 or 6, and the other accepts one or two groups of four, giving 4 or 8. Splitting it that way rules out 5 and 7 digit values cleanly.
- How do I find hex colors inside a stylesheet?
- Drop the ^ and $ anchors, add a \b after the digits and use the g flag. That finds every hex colour in a CSS file so you can collect or replace them.