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 OverflowYou 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
you can use reg ex for this
/[,\s]+|[,\s]+/g
var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');
RegEx Explained and Demo
javascript - Remove spaces and commas at the end and beginning of comma separated string - Stack Overflow
Replacing space and comma from a string using regex
javascript - Remove white spaces from string between comma and any letter - Stack Overflow
Removing commas in a string
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);
That should work:
str = str.replace(/\s*,\s*/g, ",");
var str = " I would like to know how to use RegExp , string.match and string.replace";
console.log(
str
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
//Replace any amount of whitespace before or after a `,` to nothing
.replace(/\s*,\s*/ig, ',')
);
You will need a regular expression in both cases. You could split and join the string:
str = str.split(/[\s,]+/).join();
This splits on and consumes any consecutive white spaces and commas. Similarly, you could just match and replace these characters:
str = str.replace(/[\s,]+/g, ',');
For the trailing comma, just append one
str = .... + ',';
If you have preceding and trailing white spaces, you should remove those first.
Reference: .split, .replace, Regular Expressions
In ES6:
var temp = str.split(",").map((item)=>item.trim());
Here's a pretty simple & straightforward way to do this without needing a complex regular expression.
var str = " a , b , c ";
var arr = str.split(",").map(function(item) {
return item.trim();
});
console.log(arr)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The native .map is supported on IE9 and up: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Or in ES6+ it gets even shorter:
var str = " a , b , c ";
let arr = str.split(",").map(item => item.trim());
console.log(arr)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
ES6 shorthand:
str.split(',').map(item=>item.trim())
To replace all occurrences of a string with another string, using the following function:
str.replaceAll(',', '');
The split() method is not needed since you are not trying to turn the string into an array.
Just to remove all commas straighforwardly:
a.replace(',', '');
b.replace(',', '');
Split on any sequence of spaces and commas:
str.split(/[ ,]+/).join(',')
You might also want to use filter to remove empty strings:
str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')
Another solution would be to match any sequence that does not contain a space or comma:
str.match(/[^ ,]+/g).join(',')
Use the String.replace() method.
var newString = yourString.replace(/[ ,]+/g, ",");
This says to replace any sequence of one or more spaces or commas in a row with a single comma, so you're covered for strings where the commas have spaces around them like "test, test,test test test , test".
(Note: if you want to allow for other whitespace characters beyond just a space use \s in the regular expression: /[\s,]+/g)