在string.replace中使用具名组匹配
let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
let re = '2015-01-02'. replace (reg , (
matched,//整个匹配结果
capture1,
capture2,
capture3,
position,//匹配开始的位置
s,//原字符串
groups//具名组构成的对象,{year, month, day}
) => {
let {day, month, year} = groups
; return `${day}/${month}/${year}`; }) ;
callback的参数也可以使用rest参数的写法
浙公网安备 33010602011771号