TypeScript string replace with regex, groups and partial string
You can do it using
function formatStr(str){
str = str.replace(/(\d{1,3})(\d{0,3})(\d{0,3})(\d{0,2})/g, function(a, b, c, d, e){
let ret = "";
if(b != "")
ret = b;
if(c != "")
ret = ret+"." + c;
if(d != "")
ret = ret+"." + d;
if(e != "")
ret = ret+"-" + e;
return ret;
})
console.log(str);
}
formatStr('12312312312');
formatStr('1231231');