在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参数的写法

作者:冯亮
         
能力有限,水平一般。如有错误,欢迎指正
原文地址:https://www.cnblogs.com/fengliang/p/12559352.html