Regular Expression for formatting numbers in JavaScript

前端 未结 14 1514
遥遥无期
遥遥无期 2020-11-30 19:35

I need to display a formatted number on a web page using JavaScript. I want to format it so that there are commas in the right places. How would I do this with a regular exp

14条回答
  •  感情败类
    2020-11-30 20:32

    I think you would necessarily have to do multiple passes to achieve this with regular expressions. Try the following:

    1. Run a regex for one digit followed by 3 digits.
    2. If that regex matches, replace it with the first digit, then a comma, then the next 3 digits.
    3. Repeat until (1) finds no matches.

提交回复
热议问题