If you want to convert all hex colours into decimal RGB values in the string str
, the following will do the trick. Note that this only considers 8-bit/channel hex values (e.g., #FF0000
) and not the 4-bit variants (e.g., #F00
); however, this would be easy enough to enhance.
var rgbHex = /#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])/gi
str.replace(rgbHex, function (m, r, g, b) {
return 'rgb(' + parseInt(r,16) + ','
+ parseInt(g,16) + ','
+ parseInt(b,16) + ')';
})