JS 正则表达式转换字符串

获取第一个.前面的字符串,以及后面的字符串:

const transform = str => {
  str.replace(/([^\.]*)\.(.*)/, function($0, $1,$2){
    // $0是匹配的完整的字符串
    console.log($1,":", $2);
  });
}
transform("abc.def.ghi")
// abc:def.ghi

或者

const transform = str => {
  str.replace(/(.*)(?:\.)(.*)/, function ($0, $1, $2) {
    console.log($1, ":", $2);
  });
}
posted @ 2019-09-21 11:47  水郁  阅读(4767)  评论(0编辑  收藏  举报
……