HomeRegex LibraryHex Color Code Regex (#RGB and #RRGGBB)
100% Client-Side β€’ 0 B Data Leaves Browser
web

Hex Color Code Regex (#RGB and #RRGGBB)

Matches 3-digit or 6-digit CSS hexadecimal color notation.

Regular Expression Pattern:
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i
Valid Match Example:
#10b981
Invalid / Non-Match Example:
#12345

Regex Syntax Breakdown & Explanation

  • β€’^# : Requires starting hash symbol
  • β€’[A-Fa-f0-9]{6} : Matches full 6-digit hex format
  • β€’| : Or
  • β€’[A-Fa-f0-9]{3} : Matches shorthand 3-digit hex format

Implementation in Popular Languages

JavaScript / TypeScript
const regex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i;
const isValid = regex.test("#10b981");
console.log(isValid); // true
Python (re)
import re

pattern = r"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
match = re.match(pattern, "#10b981")
print(bool(match)) # True
Go (regexp)
package main
import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile(`^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$`)
  fmt.Println(re.MatchString("#10b981"))
}
Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

The regular expression pattern is /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i. Matches 3-digit or 6-digit CSS hexadecimal color notation.