Regex for RGB Color
This regex matches CSS RGB color function syntax in the traditional comma-separated format. Each color channel value is 1-3 digits (0-255 range is not enforced by the regex). The pattern allows optional whitespace around the commas for flexible formatting. RGB is one of the most widely used color models in web development and digital design.
^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$ What is the regex pattern for RGB Color?
The regex pattern for RGB Color is ^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$ with the i flag. This regex matches CSS RGB color function syntax in the traditional comma-separated format. Each color channel value is 1-3 digits (0-255 range is not enforced by the regex). The pattern allows optional whitespace around the commas for flexible formatting. RGB is one of the most widely used color models in web development and digital design. This pattern is commonly used for css color validation and color manipulation.
Test Examples
rgb(255, 99, 71) rgb(255, 99, 71) rgb(0,0,0) rgb(0,0,0) rgb(255 99 71) Common Uses
- ✓ CSS color validation
- ✓ Color manipulation
- ✓ Design system tokens
- ✓ Theme generation
Variations
With alpha (rgba)
^rgba\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*(0|1|0?\.\d+)\s*\)$ Includes alpha channel as 0-1 decimal
Modern syntax (spaces)
^rgb\(\s*\d{1,3}\s+\d{1,3}\s+\d{1,3}\s*\)$ CSS Color Level 4 space-separated syntax
Percentage values
^rgb\(\s*\d{1,3}%\s*,\s*\d{1,3}%\s*,\s*\d{1,3}%\s*\)$ Uses percentage values (0%-100%)
Frequently Asked Questions
What is the range for RGB values?
Each channel (Red, Green, Blue) ranges from 0 to 255. This regex matches the format but does not enforce the 0-255 range. Values like rgb(999, 999, 999) will match but are invalid colors.
What is the difference between rgb() and rgba()?
rgba() adds a fourth parameter for alpha (opacity), ranging from 0 (fully transparent) to 1 (fully opaque). In modern CSS, rgb() can also accept an alpha value using the slash syntax: rgb(255 99 71 / 0.5).
Should I use rgb() or hex colors in CSS?
Both are valid. Hex is more compact, while rgb() is more readable and easier to manipulate programmatically. Modern CSS also supports hsl(), oklch(), and other color spaces that may be more intuitive for certain use cases.