I was wrestling with this issue today and have a slightly more simplified solution to this issue which uses a slightly different Regular Expression along with JavaScripts 'match' function (I have wrapped it up in a function so that we can reuse this functionality as required):
function parseRGB ( string ) {
var rgbRegex = /(rgb\([^)]*\))/gi;
var rgbArray = string.match(rgbRegex);
return rgbArray;
}
Now simply call the function with the value you want to check. E.g.
parseRGB( '-moz-linear-gradient( top, rgb(68, 68, 68), rgb(153, 153, 153))' );
Which will return the following array:
[ "rgb(68, 68, 68)", "rgb(153, 153, 153)" ]
A few things to note:
- If no RGB matches are found this function will return null.
- JavaScripts match string function will automatically put all occurances into an array for you. It can take a regular expression as a parameter.
- This can handle multiple rgb values if they exist in the string to be checked.
Hope this helps someone.