Regex for RGB Color
Regex Pattern
^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$CSS RGB color function rgb(r, g, b)
Quick Answer
The regex pattern for rgb color is `^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$`. CSS RGB color function rgb(r, g, b). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| rgb(255, 0, 0) | ✓ Matches |
| rgb(0,0,0) | ✓ Matches |
| rgb( 128 , 128 , 128 ) | ✓ Matches |
| rgb(256, 0, 0) | ✗ No match |
| rgba(0,0,0,1) | ✗ No match |
| #FF0000 | ✗ No match |
Code Examples
javascript
const regex = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/;
const isValid = regex.test(value);python
import re
pattern = r'^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/', $value)) {
echo "valid";
}java
String pattern = "^rgb\\(\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*\\)$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
Positive Integer
Integers greater than zero (no leading zeros)
Non-negative Integer
Zero or positive integer
Decimal Number
Optionally signed decimal number
Hex Color Code
3 or 6-digit hex color with optional #
USD Currency
US dollar amount with optional $ and thousands separators
Hex Color with Alpha
Hex color code with optional alpha channel (3, 4, 6, or 8 digits)