Wrox Programmer Forums Number formatting / RegExp
 |
 Javascript General Javascript discussions.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

March 3rd, 2008, 01:50 PM
 pierre.voisin Registered User Join Date: Feb 2008 Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts
Number formatting / RegExp

Hi! :)

I would like to format a number like 1500 into "1,500", for example. It should produce either "1,500,000" with 1500000. So I thought to use regular expressions and I met this eternal problem...

Admitting we are using the following expression to match and transform numbers:
^(\d{0,2})(\d{3})*\$

With the following replacement: \$1,\$2

Well, for numbers < 1M it is working. But, for numbers >= 1M it does not suffice. Does someone know if it is possible, and how, to identify the matching occurences of the pattern (\d{3})* in my expression?
Indeed, the result for 1500000 will be "1,000" as the [u]last match</u> of the pattern will be the last three zeros. Finally, I don't want to capture these only ending zeros but all the repetitions of three-number groups in order to dump them separated with a comma.

NOTE: using regular expressions for this case is important to me since I should be able to implement some little stuff to format the numbers like I want to. I would like to learn more on this kind of capturing matters if someone could teach me.;)

Thank you!

 Similar Threads Thread Thread Starter Forum Replies Last Post Textbox Number Formatting pencilneck Visual Basic 2005 Basics 2 October 25th, 2007 03:46 PM Problems with Number Formatting Roly Reefer Access 1 July 10th, 2005 02:00 PM formatting number vkey XSLT 1 June 22nd, 2005 02:29 AM formatting number dhol General .NET 2 February 24th, 2005 12:47 PM Formatting a number eelisMX Pro VB.NET 2002/2003 7 January 28th, 2005 05:22 AM