You can just replace every space and comma with space then trim those trailing spaces:

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";
res = str.replace(/[, ]+/g, " ").trim();

jsfiddle demo

Answer from Jerry on Stack Overflow
🌐
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.
Discussions

Removing commas in a string
Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence'; More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
September 18, 2019
javascript - Remove leading comma from a string - Stack Overflow
I have the following string: ",'first string','more','even more'" I want to transform this into an Array but obviously this is not valid due to the first comma. How can I remove the first comma fr... More on stackoverflow.com
🌐 stackoverflow.com
Remove commas from the string using JavaScript - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
[javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
Trim after the split with map, so str.split(",").map((s) => s.trim()) Much easier to read than regex imo More on reddit.com
🌐 r/webdev
7
1
September 14, 2021
People also ask

What is the easiest way to remove commas from a string in JavaScript?
The easiest method is str.replaceAll(',', ''), available since ES2021. It removes every comma in one line with no regex needed. For older browser support, use str.replace(/,/g, '') instead — the /g flag makes it replace globally, not just the first occurrence.
🌐
itsourcecode.com
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - ...
Can I remove commas from a number directly in JavaScript?
No — you can only remove commas from strings, because JavaScript numbers don't contain commas internally. If you have a string like '1,234.56' representing a number, strip the commas first with str.replace(/,/g, ''), then convert: parseFloat(str.replace(/,/g, '')).
🌐
itsourcecode.com
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - ...
What is the difference between replace() and replaceAll() in JavaScript?
replace() only replaces the first occurrence when given a string argument. replaceAll() always replaces all occurrences. Both work the same way with regex (when using /g flag), but replaceAll() is clearer and requires ES2021+ browsers.
🌐
itsourcecode.com
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - ...
🌐
freeCodeCamp
forum.freecodecamp.org › programming › javascript
Replacing space and comma from a string using regex - JavaScript - The freeCodeCamp Forum
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: “23,45,567”.replace(‘/[1]$/g’, ‘’) “23,45,567”.replace(‘/[2]+$/g’, ‘’) “23,45,567”.replace(‘/^\s,$/g’, ‘’) And many ...
🌐
Java2Blog
java2blog.com › home › core java › remove comma from string in javascript
Remove Comma from String in JavaScript - Java2Blog
May 24, 2022 - Replacement String – String with which we want to replace matched string. Let’s go through regular expression /\,/g used in above replace method. The forward slashes mark start and end of regular expression · We have used \ before , to escape it as , has special meaning in regular expression. g denotes global replacement here. It is flag at end of regex which depicts that we want to remove all the commas not only first one.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-all-commas-from-string
Remove/Replace all Commas from a String in JavaScript | bobbyhadz
Strings are immutable in JavaScript. We remove all commas from the string by replacing them with empty strings.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Removing commas in a string - JavaScript - The freeCodeCamp Forum
September 18, 2019 - Hey guys, i have a basic js question here… how can i remove commas in a string and replace them with a space. let myString = 'hello,this,is,a,difficult,to,read,sentence';
Find elsewhere
🌐
Linux Hint
linuxhint.com › remove-commas-from-string-in-javascript
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Delft Stack
delftstack.com › home › howto › javascript › remove commas from string javascript
How to Remove Commas From String in JavaScript | Delft Stack
February 2, 2024 - The replace() method accepts a regular expression pattern and a replacement string. It’ll use the regular expression pattern to find the commas in the string, replacing the commas with the replacement string.
🌐
LinkedIn
linkedin.com › pulse › how-remove-comma-from-string-javascript-xaufc
How to remove comma from string in JavaScript
March 13, 2024 - -> The second argument is an empty string (""). This replaces all commas with nothing, effectively removing them.
🌐
Itsourcecode
itsourcecode.com › home › how to remove commas from string javascript? 3 methods
How To Remove Commas From String JavaScript? 3 Methods - Itsourcecode.com
May 13, 2026 - Learn How To Remove Commas From String JavaScript with help of 3 methods namely, replace method, join and split method and regular expression.
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to remove first comma from string in javascript
How to Remove First Comma from String in Javascript
December 18, 2023 - To remove the first comma from a string in JavaScript, we can make use of the replace() method along with a regular expression.
🌐
Reactgo
reactgo.com › home › how to remove commas from a string in javascript
How to remove commas from a string in JavaScript | Reactgo
August 16, 2021 - In the example above, we have passed two arguments to the replace() method, the first one is regular expression /,/g, the second one is an empty string "". so it replaces all instances of a comma with an empty string.
🌐
Oamatech
oamatech.com › javascript-remove-comma-from-string
Loading...
November 11, 2020 - We cannot provide a description for this page right now
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
}
🌐
Reddit
reddit.com › r/webdev › [javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
r/webdev on Reddit: [javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
September 14, 2021 -

i think this can be done by regEx by i don't how to look it up.

this is a learning project, but i am just thinking about the scale

i have a form where the user enter a bunch of categories and i want the user to separate those categories with a comma, but working on the case where the user add the comma but also a space after the comma (as we all do) or before the comma, how to go about treating this case, because i don't want to end up with two or three categories that are the same.

edit: i did it but i don't want to remove the post to help anyone with the same issue.

here's what i did

const categoriesAsString = e.target.value;

const categoriesTrimmed = categoriesAsString.trim();

const categoriesAsStringWithWhiteSpace = categoriesTrimmed.replace(/\s*,\s*/g,",");

const categoriesAsArray = categoriesAsStringWithWhiteSpace.split(",");

setCategories(categoriesAsArray);

🌐
The Web Dev
thewebdev.info › home › how to remove a leading comma from a javascript string?
How to Remove a Leading Comma from a JavaScript String? - The Web Dev
April 23, 2021 - We can use various string methods to get rid of the leading comma from a string. How to Remove All Line Breaks From a JavaScript String? String.prototype.replace We can call the replace method with a regex to search for all line…