使用正则表达式格式化金额
2020-01-21 本文已影响0人
holidayPenguin
var val='12312.235423'
var rex = /\d{1,3}(?=(\d{3})+$)/g
val.replace(/^(-?)(\d+)((\.\d+)?)$/, function (s, s1, s2, s3) {
return '$' + s1 + s2.replace(rex, '$&,') + s3
})
//"$12,312.235423"
var val='12312.235423'
var rex = /\d{1,3}(?=(\d{3})+$)/g
val.replace(/^(-?)(\d+)((\.\d+)?)$/, function (s, s1, s2, s3) {
return '$' + s1 + s2.replace(rex, '$&,') + s3
})
//"$12,312.235423"