CSS Custom Property (Variable)
Match CSS custom properties (variables) like `--brand-color` or `--font-size-lg`.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("--[a-zA-Z][a-zA-Z0-9\\-]*", "g");
const input = ":root { --brand-color: #10b981; --space-4: 1rem; }";
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
import re
pattern = re.compile(r"--[a-zA-Z][a-zA-Z0-9\-]*")
input_text = ":root { --brand-color: #10b981; --space-4: 1rem; }"
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
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.
Pattern
--[a-zA-Z][a-zA-Z0-9\-]* (flags: g)Raw source: --[a-zA-Z][a-zA-Z0-9\-]*
How it works
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
—Common use cases
- •Design-token extraction from CSS files
- •Theme migration tooling
- •Dead-CSS-variable detection
- •Documentation generation for design systems
Related patterns
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.
CSS Class from `class=""` Attribute
WebExtract the value of a `class="..."` attribute from raw HTML, handling double or single quotes.
Hex Color Code
WebMatch CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.