Javascript Regular Expression for rgb values
Try this regex:
/rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/
It will capture the r value into $1, the g value into $2, and the b value into $3
I have come up with this "^(rgb)?\(?([01]?\d\d?|2[0-4]\d|25[0-5])(\W+)([01]?\d\d?|2[0-4]\d|25[0-5])\W+(([01]?\d\d?|2[0-4]\d|25[0-5])\)?)$"
which can validate a whole heap of string variations including:
- rgb(255,255,255)
- rgb(255, 255, 255) rgb(0/0/0)
- rgb(50-50-50)
- rgb(0 - 0 - 0)
- rgb(255,0-50)
- rgb(0, 255 255)
- rgb( 0 0 0 )
- 255,255,255
- 255, 255, 0
- (0, 0, 30)
- (255 - 255 - 255)
- rgb0 0 0
- rgb255 - 0/255
Here is some example code that should do approximately what you need or set you in the right direction:
var color = 'rgb(255, 15, 120)';
var matchColors = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
var match = matchColors.exec(color);
if (match !== null) {
document.write('Red: ' + match[1] + ' Green: ' + match[2] + ' Blue: ' + match[3]);
}
You can see it in action here: http://jsfiddle.net/xonev/dRk8d/