generate a whitespace after a comma with a regular expression

I need to generate a regular expression that generates a blank space after a comma only when there is a word, if there is no word I do not want it to generate it.

The code below does not do what I want

let regex = new RegExp("\,[ \t]*");
let example = "This is my car,and my motorbike"
let example2 = "This is my car,"
let newexmpl= example.replace(regex,", ");
let newexmpl2= example2.replace(regex,", ");
console.log(`"${newexmpl}"`);
console.log(`"${newexmpl2}"`);

47 thoughts on “generate a whitespace after a comma with a regular expression”

Leave a Comment