在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} = args[args.length - 1] ; return `${day}/${month}/${year}`;
    }) ;

  callback的参数也可以使用rest参数的写法

猜你喜欢

转载自www.cnblogs.com/fengliang/p/12559352.html