獲取第一個.前面的字符串,以及後面的字符串:code
const transform = str => { str.replace(/([^\.]*)\.(.*)/, function($0, $1,$2){ // $0是匹配的完整的字符串 console.log($1,":", $2); }); } transform("abc.def.ghi") // abc:def.ghi
或者orm
const transform = str => { str.replace(/(.*)(?:\.)(.*)/, function ($0, $1, $2) { console.log($1, ":", $2); }); }