CSS rgb() Color
Match CSS rgb() and rgba() color functions, including an optional alpha channel.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("rgba?\\(\\s*\\d{1,3}\\s*,\\s*\\d{1,3}\\s*,\\s*\\d{1,3}(?:\\s*,\\s*(?:0|1|0?\\.\\d+))?\\s*\\)", "gi");
const input = "rgb(255, 87, 51)";
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"rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)", re.IGNORECASE)
input_text = "rgb(255, 87, 51)"
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(`(?i)rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)`)
input := `rgb(255, 87, 51)`
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
rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\) (flags: gi)Raw source: rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)
How it works
Examples
Input
rgb(255, 87, 51)Matches
rgb(255, 87, 51)
Input
rgba(0, 0, 0, 0.5)Matches
rgba(0, 0, 0, 0.5)
Input
color: blue;No match
—Common use cases
- •Stylesheet color extraction
- •Design token migration
- •Theme color auditing
- •CSS-in-JS transform tooling
Related patterns
CSS hsl() / hsla() Color
WebMatch CSS hsl() and hsla() color functions, capturing hue (0–360), saturation, lightness, and optional alpha.
Hex Color Code
WebMatch CSS hex color codes in both 3-digit (#RGB) and 6-digit (#RRGGBB) formats.
CSS Custom Property (Variable)
WebMatch CSS custom properties (variables) like `--brand-color` or `--font-size-lg`.
HTML5 Color Input Value
WebValidate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.