Search code examples
javascriptregexstringstr-replace

Remove white spaces from string between comma and any letter


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 javascript.

var str= " I would like to know how to use RegExp    ,    string.match    and  string.replace"

I would like to delete all white spaces BETWEEN comma and any letter.So after that this string will look like this.

    str= " I would like to know how to use RegExp,string.match    and  string.replace"

I only know how to delete all white spaces from string using this-->

str = str.replace(/\s/g, "")

Solution

  • That should work:

    str = str.replace(/\s*,\s*/g, ",");