日期:2014-05-16 浏览次数:20355 次
<script> function onKeyPrice(t) { var stmp = ""; if(t.value==stmp) { return; } var ms = t.value.replace(/[^\d\.]/g,"").replace(/(\.\d{2}).+$/,"$1").replace(/^0+([1-9])/,"$1").replace(/^0+$/,"0"); var txt = ms.split("."); while(/\d{4}(,|$)/.test(txt[0])) { txt[0] = txt[0].replace(/(\d)(\d{3}(,|$))/,"$1,$2"); } t.value = stmp = txt[0]+(txt.length>1?"."+txt[1]:""); } </script> <input type="text" name="amount" onkeyup="onKeyPrice(this)">
------解决方案--------------------
a.replace(/(.{3})/g,'$1,')
------解决方案--------------------
//整理了一下,写成函数形式了 function addComma(n) { var ar = n.toString().split('').reverse(); var result = []; for (var i = 0; i < ar.length; i ++) (i + 1) % 3 == 0 && i < ar.length - 1 ? result.push(',' + ar[i]) : result.push(ar[i]); return result.reverse().join(''); } alert(addComma(123456789));
------解决方案--------------------
很简单的正向预查:
"12345678".replace(/.(?=(...)+$)/g, "$&,")
------解决方案--------------------