/*
描述
给定字符串 str,检查其是否包含 连续3个数字,请使用正则表达式实现。
1、如果包含,返回最先出现的 3 个数字的字符串
2、如果不包含,返回 false
示例1
输入:
'9876543'
输出:
987
*/
/* function rgb2hex(sRGB) {
const reg = /^rgb\((\d{1,3}\,\s*){2}\d{1,3}\)$/
if(!reg.test(sRGB)) return sRGB
const reg1 = /\d{1,3}/g
const newArr = sRGB.match(reg1)
let str = '#'
console.log(newArr)
newArr.forEach(item=>{
str+=Number(item).toString(16)
})
return str
} */
function rgb2hex(sRGB) {
return sRGB.replace(/^rgb\((\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\)$/g, function(a, r, g, b,e){
console.log(a,r,g,b,e)
return '#' + hex(r) + hex(g) + hex(b);
});
}
function hex(n){
return n < 16 ? '0' + (+n).toString(16) : (+n).toString(16);
}
console.log(rgb2hex('rgb(0, 0, 0)'))
console