Go (RE2)

CSS hsl() / hsla() Color in GO

Match CSS hsl() and hsla() color functions, capturing hue (0–360), saturation, lightness, and optional alpha.

Try it in the GO tester →

Pattern

regexGO
hsla?\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)   (flags: gi)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?i)hsla?\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)`)
	input := `hsl(200, 100%, 50%)`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

hsla? matches hsl or hsla. The three numeric groups capture hue (degrees), saturation %, and lightness %. The optional fourth group captures an alpha value of 0, 1, or a decimal like 0.75. Whitespace is allowed around all values.

Examples

Input

hsl(200, 100%, 50%)

Matches

  • hsl(200, 100%, 50%)

Input

hsla(120, 60%, 70%, 0.8)

Matches

  • hsla(120, 60%, 70%, 0.8)

Input

color: blue;

No match

Same pattern, other engines

← Back to CSS hsl() / hsla() Color overview (all engines)