Javascript Regular Expression for rgb values

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/

Leave a Comment