function addTotal(checkValue, openAmount) {
if(checkValue)
totalOpenAmount += openAmount;
else
totalOpenAmount -= openAmount;
if(blnUpdateCheckAll) updateAllChecked();
document.getElementById('divTotalPayAmount').innerHTML = totalOpenAmount.toFixed(2).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}这个(/(\d)(?=(\d{3})+(?!\d))/g)是如何处理这个函数的?一个数字至少是四位数?
发布于 2018-05-23 02:25:46
(\d)(?=(\d{3})+(?!\d))将从数字字符串中的最后3位数字和该数字字符串的最后3位数字的末尾转换为每3位数字。我不清楚它在代码中是如何使用的,但这就是regex。
发布于 2021-08-08 17:15:24
当它在代码中使用时,逗号",“将连接在一个三位数字的数字字符串之后。请检查代码:
var n = 1020304050;
document.getElementById('number').innerHTML = "This is a number: " +n;
var output = n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
document.getElementById('result').innerHTML = "After using the regex the result is: "+output;
console.log(output);
/*
This is a number: 1020304050
After using the regex the result is: 1,020,304,050
*/<html>
<body>
<h3 id="number"></h3>
<h3 id="result"></h3>
</body>
</html>
https://stackoverflow.com/questions/50478755
复制相似问题