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
๐ŸŒ
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 ...
Discussions

[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
javascript - How to remove all commas from a string and add white space? - Stack Overflow
I need to be able to remove ALL commas from those strings and keep the white space in order to have: var resultA = "November 5 1916"; var resultB = "October 5โ€“10 1592"; ... I do need the split() afterwards as I need each string in an array. ... I'm no js expert, but your calls to replace ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
regex - JavaScript remove fullstop, comma and spaces from a string - Stack Overflow
I'm trying to remove the fullstops, commas and spaces from a string. I'm pretty sure I have to do this using this: str.replace( , ""); I'm just not very familiar with Regex,... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 24, 2016
regex - remove unwanted space and commas in JavaScript - Stack Overflow
I want to remove all unnecessary spaces and commas from the start/end of the string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ split-the-sentences-by-comma-and-remove-surrounding-spaces-javascript
Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?
October 3, 2020 - Using split(/[\s,]+/).join() is an efficient way to remove multiple consecutive commas and spaces from strings. The regular expression handles any combination of whitespace and commas, making your data clean and consistent.
๐ŸŒ
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);

๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-all-commas-from-string
Remove/Replace all Commas from a String in JavaScript | bobbyhadz
Use the `String.replaceAll()` method with a comma as the first parameter and an empty string as the second to remove all commas from a string.
Find elsewhere
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ remove-commas-spaces-string-javascript
How to Remove All Commas and Spaces from a String in JavaScript? - CodingTechRoom
Use the String.prototype.replace method with a regular expression to target commas and spaces simultaneously. Utilize the trim method to clean up leading and trailing spaces, if present.
๐ŸŒ
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';
๐ŸŒ
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.
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ core java โ€บ remove comma from string in javascript
Remove Comma from String in JavaScript - Java2Blog
May 24, 2022 - Join the String with empty String using empty space ยท This method is useful, when you want to avoid regular expressions in replace() method. ... Then joined the array elements with empty String using join() method. Thatโ€™s all about how to remove comma from string in Javascript.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? | GeeksforGeeks
JavaScript string.replace() method is used to replace a substring. With Regular expressions pattern we can find all spaces (/\s/g) and globally replace all space occurrences with an empty string.
Published ย  November 14, 2024
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ javascript-replace-spaces
JavaScript Replace Spaces
December 27, 2023 - The replace() method comes to the rescue. const stringWithNewlines = "JavaScript runs everywhere\n on everything!\n"; const stringWithSpaces = stringWithNewlines.replace(/\n/g, ' '); console.log(stringWithSpaces); // Output: "JavaScript runs ...
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Remove Spaces From String | W3Docs
If you want to remove all the space in the string, you can use the replace() method:
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ remove-extra-spaces-from-a-string-in-javascript-or-node-js
Remove Extra Spaces From a String in JavaScript or Node.js
Use JavaScriptโ€™s string.replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s. Expand the whitespace selection from a single space to multiple using the \s+ RegEx. Combine the string.replace() method and the RegEx and ...
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
}