Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for RGB / RGBA color
Regex pattern · validation

Regex for RGB / RGBA color

Regex for RGB color: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use the pattern below as an RGB / RGBA color regex. It matches "rgb(255, 0, 128)" and "rgba(0,0,0,0.5)", and rejects "rgb(256,0,0)" and "rgb(0,0)". Covers the comma syntax only.

Last updated · Maintained by FreeAIToolz · How we test

/^rgba?\(\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$/
  • rgb(255, 0, 128)matches
  • rgba(0,0,0,0.5)matches
  • rgb( 12 , 34 , 56 )matches
  • rgb(256,0,0)no match
  • rgb(0,0)no match
  • rgba(0,0,0,1.5)no match
  • rgb 0,0,0no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
rgba?\(rgb( or rgba(
(?:25[0-5]|2[0-4]\d|1?\d?\d)a channel value 0–255, three times
(?:,\s*(?:0|1|0?\.\d+))?optional alpha 0–1
\)$closing parenthesis

Examples

InputResult
rgb(255, 0, 128)✓ Matches
rgba(0,0,0,0.5)✓ Matches
rgb( 12 , 34 , 56 )✓ Matches
rgb(256,0,0)✗ No match
rgb(0,0)✗ No match
rgba(0,0,0,1.5)✗ No match
rgb 0,0,0✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Covers the comma syntax only. Modern CSS also allows rgb(255 0 128 / 50%), which needs a separate pattern.

When to use this pattern

Parsing CSS colors in design tools and style linters.

Use it in your language

JavaScript

const re = /^rgba?\(\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^rgba?\(\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^rgba?\\(\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*,\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*,\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*(?:,\\s*(?:0|1|0?\\.\\d+)\\s*)?\\)$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^rgba?\\(\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*,\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*,\\s*(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)\\s*(?:,\\s*(?:0|1|0?\\.\\d+)\\s*)?\\)$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^rgba?\(\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*,\s*(?:25[0-5]|2[0-4]\d|1?\d?\d)\s*(?:,\s*(?:0|1|0?\.\d+)\s*)?\)$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for RGB / RGBA color?
See the pattern at the top of this page. Covers the comma syntax only. Modern CSS also allows rgb(255 0 128 / 50%), which needs a separate pattern.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abcrgb(255, 0, 128)xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.