var str = 123456;

var num = str.replace(/\B(?=(?:\d{3})+\b)/g, ',');

console.log(num);

如果str是数字需要 toString() 转成字符串

效果: 123,456