1,2是表示的匹配的小括号里的内容
1是匹配的第一个小括号里的 ,2是匹配的第2个小括号里的
// 将yyyy-mm-dd格式转换为年月日格式
function chDate1date(str){
var reg =/(\d{4})\-(\d{2})\-(\d{2})/;
return str.replace(reg,"$1年$2月$3日")
};
chDate1date('2019-08-28')
// $1指2019
// $2指08
// $3指28
const path = "a[0].b.c"
const paths = path
.replace(/\[(\w+)\]/g, ".$1")
.replace(/\["(\w+)"\]/g, ".$1")
.replace(/\['(\w+)'\]/g, ".$1")
.split(".");
// 其中 $1 指的是 匹配的 \w+ 即 这里的 0
// replace函数的第二个参数其他几个特殊字符串
path.replace(reg,"$&")
'a[0].b.c' // 匹配到的整个字符串
path.replace(reg,"$`") // 匹配到的字符串的左边字符串
'aa.b.c'
path.replace(reg,"$'") // 匹配到的字符串的右边字符串
在线测试地址:https://tool.oschina.net/regex/
参考文章:https://blog.csdn.net/qq_37899792/article/details/100126765