0

Possible Duplicate:
How can I format numbers as money in JavaScript?

The input string is any number which may or may not have decimal places, and we have to

place commas in appropriate places in the currency number. For example-

input - 1000        output - 1,000

input - 1000.00     output - 1,000.00

input - 1200000     output - 1,200,000

(the comma(,) separator appears after every 3 digits, ignoring decimal places).

Can we do this total work only through regular expression in javascript?

Kindly guide.

5
  • 1
    THIS QUESTION IS NOT DUPLICATE. 149055 REFERS TO DOING THAT WITH OR WITHOUT REGEX AND ITS MONEY FORMATTING. Commented Sep 18, 2012 at 6:51
  • 3
    There is no need to be upset. That question and this question differ only by one step. You just need to use str.replace(/(\d+){4,}/g, replacer_function) and link the code from that answer. Commented Sep 18, 2012 at 6:53
  • Why do you insist on a regex solution? Commented Sep 18, 2012 at 6:54
  • Regex is not the right way to do this. Commented Sep 18, 2012 at 7:02
  • "Can we do this total work only through regular expression in javascript?" No. But you can use .replace() in combination with a regular expression. If you don't like the other question that Korvin linked to, how about stackoverflow.com/questions/6392102/… (some of its answers use regex). And you should look at the list of questions under the "Related" heading on the bottom right of this page. Commented Sep 18, 2012 at 7:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.