正则表达式:数值及数值型字符串三位一组格式化

前端之家收集整理的这篇文章主要介绍了正则表达式:数值及数值型字符串三位一组格式化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<body>
<div id="container"></div>
<script>
var formatNumber = function(str,split){
		var type = typeof str;
		switch (type){
			case "number":
				str = str.toString();
			case "string":
				return str.replace(/(\d+?)(?=(?:\d{3})+$)/g,"$1"+split);
				break;
			default:
				return ;
		}		
	},arr=["0.3000","22.356","444.456",123456.56,"1","111","1211","123456","1457910","aaaa",{"name":"zhangsan"}],str="<table width='50%' cellspacing='0' cellpadding='0' border='1' align='center'>";
for(var i = 0; i< arr.length;i++){
	var ss = arr[i],format = formatNumber(ss,",");
	str += "<tr><td>"+ss+"</td><td>"+format+"</td></tr>";
}
str+="</table>";
document.getElementById("container").innerHTML = str;
</script>
</body>


结果如下图:

猜你在找的正则表达式相关文章