How about doing the test.replace with a negative lookahead - https://regex101.com/r/WtHcuO/2/:

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "");

console.log(stripped);

Or, if you want to preserve the commas, but escape them, you can replace with \\, instead of ""

var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);

var stripped = data.replace(/,(?!["{}[\]])/g, "\\,");

console.log(stripped);

Answer from combatc2 on Stack Overflow
Discussions

javascript - How to remove commas from json object displayed in HTML? - Stack Overflow
I just want to remove commas from object displayed in DOM. Actually, I'm getting ... Maybe you should display it as a table or list, not just plain text. ... Can you give us the actual structure of the "JSON object"? More on stackoverflow.com
🌐 stackoverflow.com
November 10, 2017
Remove commas in numbers for JSON output
Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output. More on googlecloudcommunity.com
🌐 googlecloudcommunity.com
14
1
November 11, 2019
javascript - JSON Remove trailing comma from last object - Stack Overflow
This JSON data is being dynamically inserted into a template I'm working on. I'm trying to remove the trailing comma from the list of objects. The CMS I'm working in uses Velocity, which I'm not too More on stackoverflow.com
🌐 stackoverflow.com
Remove comma from number in array.
Just create a new array with your values: return [months, removeCommaFromNumber] // instead of return months + removeCommaFromNumber More on reddit.com
🌐 r/learnjavascript
3
1
March 30, 2022
🌐
JetBrains
youtrack.jetbrains.com › issue › WEB-19143 › Remove-last-comma-in-json-or-js-arrays-object-automatically
Remove last comma in json or js arrays/object automatically
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
OfflineTools
offlinetools.org › a › json-formatter › handling-trailing-commas-in-json-a-formatters-approach
How to Remove Trailing Commas From JSON | Offline Tools
If your goal is simply to make the JSON valid again, use this order of operations: Find the closing } or ] where parsing fails. Check the previous non-whitespace character. If that character is a comma, delete it.
🌐
YouTube
youtube.com › dritalconnect
How to remove comma from number from string in JavaScript - YouTube
How to remove comma from number from string in JavaScript
Published   May 30, 2022
Views   910
🌐
Google Cloud
googlecloudcommunity.com › appsheet › appsheet q&a
Remove commas in numbers for JSON output - AppSheet Q&A - Google Developer forums
November 11, 2019 - Is there a way to format a number without commas in the template used for JSON output? I don’t want to lose the commas in the app, just in the JSON file output.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › remove comma from number in array.
r/learnjavascript on Reddit: Remove comma from number in array.
March 30, 2022 -

Hi all, I'm trying to remove commas from my numbers that are in an array :

I know the method '.toFixed(2)' but I'm having trouble applying it in a .map

This is what I did:

And this is what I get :

Unfortunately I added the two values ​​so I don't get an array anymore.

What would be the right way to find an array like I had but without a comma?
How do I merge them into my array? 🙂

🌐
GitHub
gist.github.com › ozzi- › 4eefe81a224f4fbfaaf415010fd213ee
removes trailing commas in JSON strings · GitHub
removes trailing commas in JSON strings · Raw · JSON - Remove Trailing Comma · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Microsoft Power Platform Community
powerusers.microsoft.com › t5 › Building-Flows › How-to-remove-comma-from-a-json-string-within-double-quotes › td-p › 1809430
Forums | Microsoft Power Platform Community
October 10, 2022 - From forums to user groups to gallery samples, this community has something for everyone— across all Power Platform products.
🌐
JSON Prism
jsonprism.com › home › learn › trailing commas in json: why they break parsing
Fix Trailing Commas in JSON (Why They Break) | JSON Prism
June 16, 2026 - The error you'll see from JSON.parse() is typically: ... The position points at (or just after) the offending comma. One file, quickly: paste it into the JSON Trimmer. It removes trailing commas, comments, and other JSON5-style extras, returning ...
🌐
FormatArc
formatarc.com › blog › json trailing comma: why it fails and how to remove it (rfc 8259)
JSON Trailing Comma: Why It Fails and How to Remove It (RFC 8259) | FormatArc
3 weeks ago - The error message points at the closing bracket that follows the stray comma, so delete the comma on the line just before it and run again. To prevent it entirely, generate JSON with JSON.stringify instead of building strings by hand. JavaScript object and array literals permit trailing commas as a language convenience.
Top answer
1 of 3
239

To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:

var total = parseFloat('100,000.00'.replace(/,/g, '')) +
            parseFloat('500,000.00'.replace(/,/g, ''));
2 of 3
7

Related answer, but if you want to run clean up a user inputting values into a form, here's what you can do:

const numFormatter = new Intl.NumberFormat('en-US', {
  style: "decimal",
  maximumFractionDigits: 2
})

// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123

// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24

// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN

// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN

Use the international date local via format. This cleans up any bad inputs, if there is one it returns a string of NaN you can check for. There's no way currently of removing commas as part of the locale (as of 10/12/19), so you can use a regex command to remove commas using replace.

ParseFloat converts the this type definition from string to number

If you use React, this is what your calculate function could look like:

updateCalculationInput = (e) => {
    let value;
    value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
    if(value === 'NaN') return; // locale returns string of NaN if fail
    value = value.replace(/,/g, ""); // remove commas
    value = parseFloat(value); // now parse to float should always be clean input

    // Do the actual math and setState calls here
}
🌐
Medium
frontendinterviewquestions.medium.com › how-to-remove-comma-from-string-in-javascript-21c88f166c28
How to remove comma from string in JavaScript | by Pravin M | Medium
March 13, 2024 - let myString = "This, is, a, string, with, commas."; let newString = myString.replace(/,/g, ""); console.log(newString); // Output: This is a string with commas.
🌐
Snipplr
snipplr.com › view › 73098 › addingremove-commas-in-digits
Adding/remove commas in digits - JavaScript Snipplr Social Repository
December 6, 2013 - Ex: 1,000 Useful for grabbing input values to format then display, remove the comma and convert back to number to do math.
Top answer
1 of 6
8

What makes this a non-trivial problem is that the JSON format does not care about whitespace that does not occur inside keys or data. Therefore,

{ "key": "data" }

is the same as

{ "key":
"data"
}

If you add the possibility of a "broken" JSON file, such as

{ "key":
"data", }

it becomes really difficult to properly parse the document with anything other than a JSON parser that knows how to relax the restrictions of the JSON format when parsing the data.

The Perl JSON module can do that, and also pretty-print the result:

$ cat file.json
{
    "fruit": "Apple",
}
$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}), {pretty=>1})' file.json
{
   "fruit" : "Apple"
}

Here, we read in the whole text document into the array @text. We then decode this while relaxing the parsing (this enables the JSON document to have commas before } and ] and also to include # comments). We then immediately encode the resulting Perl data structure into JSON again and print it.

Another example:

$ cat file.json
{
    "fruit": "Apple",   # a comment
    "stuff": [1, 2, 3,],
}
$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}), {pretty=>1})' file.json
{
   "fruit" : "Apple",
   "stuff" : [
      1,
      2,
      3
   ]
}

Without pretty printing:

$ perl -MJSON -e '@text=(<>);print to_json(from_json("@text", {relaxed=>1}))' file.json
{"fruit":"Apple","stuff":[1,2,3]}

(no newline at the end of the output)

For really large documents, you would want to use the module's incremental parsing capability and write a proper script for the conversion.

2 of 6
5

With the GNU implementation of sed:

sed -i.bak ':begin;$!N;s/,\n}/\n}/g;tbegin;P;D' FILE

sed -i.bak = creates a backup of the original file, then applies changes to the file

':begin;$!N;s/,\n}/\n}/g;tbegin;P;D' = anything ending with , followed by new line and }. Remove the , on the previous line

FILE = the file you want to make the change to

🌐
GitHub
gist.github.com › tbonemalone › 2187257
javascript: remove commas from string of numbers · GitHub
javascript: remove commas from string of numbers. GitHub Gist: instantly share code, notes, and snippets.