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 = FormNumber.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

About these ads

One Response

  1. 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: