数字每隔三位加个逗号的js正则表达式写法
这里有三种写法,如下:
function formatterNum(num) {
//return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
//return (num || 0).toString().replace(/(\d{1,3})(?=(?:\d{3})+)/g, '$1,');
return (num || 0).toString().replace(/(?!^)(\d{3})(?=(?:\d{3})*$)/g, ',$1');
}
正则表达式的学习网站:http://blog.csdn.net/zaifendou/article/details/5746988