CSS Custom Property (Variable) in GO
Match CSS custom properties (variables) like `--brand-color` or `--font-size-lg`.
Try it in the GO tester →Pattern
regexGO
--[a-zA-Z][a-zA-Z0-9\-]* (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`--[a-zA-Z][a-zA-Z0-9\-]*`)
input := `:root { --brand-color: #10b981; --space-4: 1rem; }`
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
-- is the literal double-dash that prefixes every CSS custom property. [a-zA-Z] requires the next character to be a letter (digits and hyphens are allowed elsewhere but not in lead position by convention). [a-zA-Z0-9\-]* allows the rest of the name: any combination of letters, digits, and hyphens.
Examples
Input
:root { --brand-color: #10b981; --space-4: 1rem; }Matches
--brand-color--space-4
Input
var(--text-primary, #fff)Matches
--text-primary
Input
.foo { color: red; }No match
—