转自:http://www.cnblogs.com/rubylouvre/p/3738323.html#undefined
1.不带小数点
"15000000".split("").reverse().join("").replace(/(\d{3})/g, "$1,").split("").reverse().join("");
步骤分解:
"15000000".split("")
得出[1, 5, 0, ...]
reverse()
得出 [0, 0, ... , 5, 1]
(防止出现数子个数非3倍数)
join("")
得出00000051
replace(/(\d{3})/g, "$1,")
得出000,000,51
正则中$1
匹配(\d{3})
,正好取代3个数字,再加个',
'则完美替换
把数据恢复原来顺序
"115000000".split("").reverse().join("").replace(/(\d{3})(?=[^$])/g, "$1,").split("").reverse().join("");
'1500000000000'.replace(/\B(?=(\d{3})+$)/g, ',')
2.带小数点
'123123211312.333123'.replace(/(?=(?!^)(?:\d{3})+(?:\.|$))(\d{3}(\.\d+$)?)/g,',$1');
'12000000.11'.replace(/(\d)(?=(\d{3})+\.)/g,'$1,$2,')