JavaScript: How to Remove All Commas From a Number
The following can be used in JavaScript to remove all commas from a number:
var StartNumber = ‘444,555,666‘;
var ReplacedNumber = StartNumber.replace(/\,/g,‘‘);
document.write(ReplacedNumber);
The preceding example uses the JavaScript replace function and utilizes a regular expression to replace all the commas in a number. In the regular expression, the backslash (\) before the comma is used as an escape character, meaning that the comma should be treated as a literal comma. The ‘g‘ means that the replace should be applied globally. Without this, only the first comma would be removed.
Related articles:
JavaScript replace Function
JavaScript Regular Expression Backslash
September 27, 2007 at 7:13 am
Ahh, finally a comma replace script that works for me! And it’s so simple! I kept trying other replace scripts, and they would all give me errors, but this one works great. Thanks!
June 15, 2008 at 12:44 pm
thanks !! it worked well (Y)
October 21, 2008 at 6:45 pm
Ditto JDub. This one WORKS! Thank you so much, Joey.
January 15, 2010 at 11:23 am
shouldn’t “FormNumber” in the 2nd line actually be StartNumber??
March 5, 2010 at 6:35 pm
Bubba, good catch. This has been corrected in the post.
February 10, 2010 at 11:29 pm
Worked great, thanks!