Enhancing regex of thousands separator?

后端 未结 5 1182
长发绾君心
长发绾君心 2021-01-15 03:50

I saw this beautiful script to add thousands separator to js numbers:

function thousandSeparator(n, sep)
{
    var sRegExp = new RegExp(\'(-?[0-9]+)([0-9]{3         


        
5条回答
  •  被撕碎了的回忆
    2021-01-15 04:07

    Your assumption

    now i will have $1 and $2..$n

    is wrong. You have two groups, because you have two sets of brackets.

        (-?[0-9]+)([0-9]{3})*
    1.  ^^^^^^^^^^
    2.            ^^^^^^^^^^
    

    And then you repeat the second group. If it matches the second time, it overwrites the result of the first match, when it matches the third time, it overwrites ...

    That means when matching is complete, $2 contains the value of the last match of that group.

    First approach

    (\d)(?=(?:[0-9]{3})+\b)
    

    and replace with

    $1,
    

    See it on Regexr

    It has the flaw that it does insert the comma also on the right of the dot. (I am working on it.)

    Second approach

    (\d)(?:(?=\d+(?=[^\d.]))(?=(?:[0-9]{3})+\b)|(?=\d+(?=\.))(?=(?:[0-9]{3})+(?=\.)))
    

    and replace with

    $1,
    

    See it on Regexr

    So now its getting a bit more complicated.

    (\d)                   # Match a digit (will be reinserted)
    (?:
        (?=\d+(?=[^\d.]))  # Use this alternative if there is no fractional part in the digit
        (?=(?:\d{3})+      # Check that there are always multiples of 3 digits ahead
        \b)                # Till a word boundary
        |                  # OR
        (?=\d+(?=\.))      # There is a fractional part
        (?=(?:\d{3})+      # Check that there are always multiples of 3 digits ahead
        (?=\.))            # Till a dot
    )
    

    Problem: does also match the fractional part if there is not the end of the string following.

提交回复
热议问题