JavaScript to Remove Dollars Signs From a Number
A common problem with HTML input forms is that users tend to include dollars signs when inputting currency numbers. These dollars signs can be easily removed through JavaScript by using the JavaScript replace function and a simple regular expression. This is demonstrated in the following example:
var Amount = '$13.22';
var ReplacedAmount = Amount.replace(/\$/g,'');
document.write(ReplacedAmount);
This example shows a dollar amount that includes a dollar sign. To remove the dollar sign, use the replace function. The first argument to the replace function is a regular expression. This regular expression states that all dollar signs in the Amount variable be found. The replace function then replaces those dollars signs with nothing.
Related articles:
JavaScript Regular Expressions
JavaScript Regular Expression Backslash
February 24, 2009 at 2:03 pm
This is a very helpful regex for removing that nasty little dollar sign. Thank you for posting this – very simple and easy to find.
Thanks!