簡體   English   中英

由於 Firefox 中的正則表達式組無效,正則表達式重寫

[英]Regex rewrite because of Invalid regexp group in Firefox

我開始僅在 Firefox 瀏覽器中收到此錯誤。 我假設這個 function 內部不受支持的后視是導致問題的原因。

Firefox 中的正則表達式組無效

我可以實現這個正則表達式的功能(千位分隔符)的另一種方法是什么?

function thousand_separator(x) {

    return x.toString().replace(/\B(?<!\,\d*)(?=(\d{3})+(?!\d))/g, ".");
}

在此處輸入圖像描述

看起來這個正則表達式可能改編自https://stackoverflow.com/a/2901298/1058183

在那個答案上倒退,試試這個:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM