//转换css中常用颜色编码 var toRGB = function (val){ var reg1 = /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i; //i不区分大小写 var reg2 = /^#([0-9A-F])([0-9A-F])([0-9A-F])$/i; var reg3 = /[0-9A-F]{2}/g,//发现第一个匹配项即停止 m; if(reg2.test(val)){ val = val.replace(reg2,"#$1$1$2$2$3$3"); }else if(reg1.test(val)){ m = val.match(reg3); val = "rgb("+[parseInt(m[0],16),parseInt(m[1],16),parseInt(m[2],16),].join(‘,‘)+")"; } return val; }; document.write(toRGB("#0000ff"));