Skip to main content
Regexflux

CSS RGB/RGBA Color

intermediate

Matches CSS rgb() and rgba() color function syntax.

colorrgbcssdesign

Pattern

/rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)/gi

Try It

0 of 6 strings matched

Explanation

Matches rgb(R, G, B) or rgba(R, G, B, A) where R/G/B are 0-999 (no range validation) and alpha is 0-1 decimal.

Test Strings

Matching

  • rgb(255, 0, 128)
  • rgba(0, 0, 0, 0.5)
  • RGB(100, 200, 50)

Non-matching

  • #ff0000
  • hsl(0, 100%, 50%)
  • rgb(255 0 128)

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOFull support

Code Snippets

const regex = /rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}(?:\s*,\s*(?:0|1|0?\.\d+))?\s*\)/gi;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Related Patterns