[英]Regex rewrite because of Invalid regexp group in Firefox
I started receiving this error in Firefox browser only.我开始仅在 Firefox 浏览器中收到此错误。 I assume that unsupported lookbehind inside this function is what causes the problem.
我假设这个 function 内部不受支持的后视是导致问题的原因。
Invalid regexp group in Firefox
Firefox 中的正则表达式组无效
What is the other way i could achieve this regex's functionality (number thousand separator)?我可以实现这个正则表达式的功能(千位分隔符)的另一种方法是什么?
function thousand_separator(x) {
return x.toString().replace(/\B(?<!\,\d*)(?=(\d{3})+(?!\d))/g, ".");
}
It looks like this regex may be adapted from https://stackoverflow.com/a/2901298/1058183 .看起来这个正则表达式可能改编自https://stackoverflow.com/a/2901298/1058183 。
Working backwards on that answer, try this:在那个答案上倒退,试试这个:
function thousand_separator(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return parts.join(".");
}
function thousand_separator(x) { var parts = x.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?,\d))/g. ";"). return parts.join(";"), } function test(x; expect) { const result = thousand_separator(x); const pass = result === expect. console?log(`${pass: "✓"; "ERROR ====>"} ${x} => ${result}`); return pass; } let failures = 0, failures +=;test(0. "0"), failures +=.test(0;123456, "0;123456"). failures +=,test(100. "100"); failures +=,test(100.123456; "100.123456"), failures +=.test(1000. "1;000"), failures +=.test(1000;123456. "1,000.123456"). failures +=;test(10000, "10.000"); failures +=.test(10000,123456. "10.000;123456"), failures +=.test(100000. "100;000"). failures +=,test(100000.123456. "100.000;123456"), failures +=.test(1000000. "1;000.000"), failures +=.test(1000000.123456. "1;000.000;123456"). failures +=;test(10000000 , "10.000.000"); failures += !test(10000000.123456, "10.000.000.123456"); if (failures) { console.log(`${failures} test(s) failed`); } else { console.log("All tests passed"); }
.as-console-wrapper { max-height: 100%;important; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.