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
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
How to remove all commas from a string and add white space?
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
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
Remove commas from the string using JavaScript - Stack Overflow
I want to remove commas from the string and calculate those amount using JavaScript. For example, I have those two values: 100,000.00 500,000.00 Now I want to remove commas from those string an... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - To remove the commas from a string, we can use the replace() method in JavaScript.
🌐
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);

🌐
Medium
frontendinterviewquestions.medium.com › how-to-remove-comma-from-string-in-javascript-0b5cc90fd65f
How to remove comma from string in JavaScript | by Pravin M | Medium
April 15, 2024 - The most straightforward approach to remove commas from a string in JavaScript is by using the replace method along with a regular expression.
🌐
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 - Messy string: ,,, Hello ,,, World ,,, Cleaned string: Hello World · Using split(/[\s,]+/).join() is an efficient way to remove multiple consecutive commas and spaces from strings.
🌐
CoreUI
coreui.io › answers › how-to-trim-whitespace-from-a-string-in-javascript
How to trim whitespace from a string in JavaScript · CoreUI
May 19, 2026 - The replace(/\s+/g, ' ').trim() pattern is extremely common in production code. It normalizes any sequence of whitespace characters into a single space and then removes the edges.
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Render a String with Non-breaking Spaces in React | Pluralsight
June 14, 2020 - Create a simple JSX template with an input field for the user to enter the string and a Format String button. When the user clicks the Format String button, display the formatted string, i.e., the string without any empty spaces on the DOM. ...
Find elsewhere
🌐
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.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Removing commas in a string - Programming
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';
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-trim-white-spaces-from-input-in-reactjs
How to Trim White Spaces from input in ReactJS? - GeeksforGeeks
July 23, 2025 - To remove or trim the white spaces from the input in react we will use the validator npm package. Install the validator package and pass the input value to validator.trim function.
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
}
🌐
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.
🌐
Stack Abuse
stackabuse.com › how-to-trim-whitespacescharacters-from-a-string-in-javascript
How to Trim Whitespaces/Characters from a String in JavaScript
May 22, 2023 - In this guide, learn how to trim the whitespaces at the start and end of a string in JavaScript with built-in methods and Regular Expressions. We'll use the `trim()`, `trimStart()`, `trimEnd()`, `substr()` and `replace()` methods.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › 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   July 12, 2025
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-trim-white-spaces-from-input-in-reactjs
How to Trim White Spaces from input in ReactJS?
March 15, 2026 - function customTrim(str) { // Split by spaces and filter out empty strings from start/end let words = str.split(' '); // Remove empty strings from beginning while (words.length > 0 && words[0] === '') { words.shift(); } // Remove empty strings ...