^ 字符串的开始位置
$ 字符串的结束位置
\ 转义字符
\d 数字
\w 字母、数字、下划线
\s 空白符
\S 非空白符
. 所有字符除了换行符\n
* 零次或多次
+ 一次或多次
? 零次或一次
{n} n次
{n,} 至少n次
{n,m} 至少n次最多m次
g 全局
i 不区分大小写
match() 找到一个或多个符合正则表达式的匹配,返回数组或null
replace() 替换与正则匹配的字符串
颜色字符串转换:
输入:‘rgb(255,255,255)'
输出:#ffffff
function rgb2hex(sRGB) { let reg = /^rgb\(\d{1,3}(,\s*\d{1,3}){2}\)$/ if(!reg.test(sRGB) { return sRGB } let list = sRGB.match(/\d{1,3}/g) let res = [] list.forEach((it) => { let value = Number(it) < 16 ? '0' + Number(it).toString(16) : Number(it) res.push(value) }) return '#' + res.join('') }