Tuesday, August 11, 2009

JavaScript Regex to Format a Monetary Amount

Found this lying around in my old notes, it's a javascript regular expression that will format a monetary string (example: $100,000.00) into a straight numeric value:

replace(/[^0-9//-//.]/g, "");


The expression will strip out any of the non-numeric characters not allowed in normal decimal arithmetic.

Example of use:

alert("$100,000.00".replace(/[^0-9//-//.]/g, ""));

produces an alert box displaying 100000.00

No comments:

Post a Comment