Archive for June 25, 2007

JavaScript: How to Remove All Commas From a Number

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on June 25, 2007 by Joey

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