金钱格式化(整数)
//正则
let cash =
'1234567890'
cash.replace(/\B(?=(\d{3})+(?!\d))/g,
','
);
//"1,234,567,890"
//非正则的优雅实现
function
formatCash(str) {
return
str.split(
''
).reverse().reduce((prev, next, index) => {
return
((index % 3) ? next : (next +
','
)) + prev
})
}
formatCash(cash);
//"1,234,567,890"