1、命名捕获
let str = '2018-03-20';
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
let {year, month ,day} = str.match(reg).groups;
console.log(year, month, day); // 2018,03,20
2、反向引用命名捕获
语法:\k<名字>
let reg = /^(?<Strive>welcome)-\k<Strive>$/; // 匹配: 'welcome-welcome-welcome'
let reg = /^(?<Strive>welcome)-\k<Strive>-\1$/; // 匹配: 'welcome-welcome-welcome'
3、替换
let str = '2018-03-20';
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
str = str.replace(reg,'$<day>/$<month>/$<year>');
console.log(str); // 20/03/2018
replace接收一个函数
let str = '2018-03-20';
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
str = str.replace(reg, (...args)=>{
//console.log(args)
let {year, month, day} = args[args.length-1];
return `${day}/${month}/${year}`;
});
console.log(str); // 20/03/2018