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
๐ŸŒ
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 - The method also handles strings ... cleaned); Messy string: ,,, Hello ,,, World ,,, Cleaned string: Hello World ยท Using split(/[\s,]+/).join() is an efficient way to remove multiple consecutive commas and spaces from ...
Discussions

javascript - Remove spaces and commas at the end and beginning of comma separated string - Stack Overflow
Given that I can get strings like this: dog, cat, person ,people, boys, girls, people, boys, police , How would I delete the possible spaces and commas at the end and beginning of strings. So for More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 24, 2017
Replacing space and comma from a string using regex
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 ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
3
0
October 26, 2020
javascript - Remove white spaces from string between comma and any letter - Stack Overflow
I use RegExp and "string".match very rarely so I'm not so sure how to use them for some little bit complex things.Here's what I would like to do and don't know how to do it. Here I have a string in 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
๐ŸŒ
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);

๐ŸŒ
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 ...
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ need-help-to-remove-space-after-comma โ€บ m-p โ€บ 3069688
Solved: Need help to remove space after comma - ServiceNow Community
October 9, 2024 - // next test: var strVal = ' X Y, Z '; (function validateModel(inputData) { // var formattedNewVal = inputData.replace(/\s*,\s*/g, ','); var formattedNewVal = inputData.replace(/^\s+|\s+$/g, ''); gs.info('new val:'+formattedNewVal+'.'); })(strVal); results in: *** Script: new val:X Y, Z. with leading and trailing spaces removed. ... Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution ... var stringVal = 'X Y, Z'; var arrayValues = stringVal.split(','); var newStringArray = []; for (i = 0; i < arrayValues.length; i++) { newStringArray[i] = arrayValues[i].trim(); } var newStringVal = newStringArray.toString(); gs.info('New value = ' + newStringVal);
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
EncodedNA
encodedna.com โ€บ javascript โ€บ how-to-remove-commas-from-array-in-javascript.htm
How to Remove Commas from Array in JavaScript
You can use the join() method in JavaScript to remove commas from an array. The comma delimiter in an array works as the separator. For example, let arr = ['alpha', 'bravo', 'charlie']. Using the join() method, you can remove or replace all the commas with a space.
๐ŸŒ
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ convert-comma-separated-string-to-array-using-javascript
JavaScript - Convert Comma Separated String To Array - GeeksforGeeks
July 11, 2025 - The filter() method removes empty strings caused by trailing commas. ... const s = "apple,banana,cherry,"; const a = s.split(",").filter(item => item !== ""); console.log(a); ... The map() method trims unnecessary spaces from each substring.