Regular Expressions 101

Community Patterns

Valid RGB color value

1

Regular Expression
ECMAScript (JavaScript)

/
^rgba?\((?:\s+)?(?<r>\d+(?<unit>%?))(?:\s+)?(?<separator>[\s|,])(?:\s+)?(?<g>\d+\k<unit>)(?:\s+)?\k<separator>(?:\s+)?(?<b>\d+\k<unit>)(?:\s+)?(?:(?<=(?:,.+))(?:,(?:\s+)?(?<a>(?:\d+)?(?:\.?\d+)))?(?:\s+)?)?\)$
/
gm

Description

Pattern for matching actually valid RGB values.

  • validate separator (mixing spaces and commas between values is not valid)
  • validate if comma is used as separator if alpha channel is set
  • validate units (mixing percentage with integers is not valid)
  • extract r, g, b, a, separator and unit with named groups
Submitted by BorisTB - 2 years ago (Last modified 2 years ago)