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 d
Simply use RegEx:
\s*,\s*
DEMO
That should work:
str = str.replace(/\s*,\s*/g, ",");
You can play around with regex expressions and get a decent documentation of the language features on https://regex101.com
Here is this.lau_'s solution of your problem: https://regex101.com/r/aT7pS5/1
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, ',')
);