phantom phantom - 6 months ago 14
Javascript Question

remove full stop or comma at end of a word

how can I remove a comma or full stop at the end of a word

for example if I want to change the following string from

var str = "This is a sentence, that I want to change. Please remove, commas also."


to

str = "This is a sentence that I want to change Please remove commas also"

Answer

You can use:

var str = "This is a sentence, that I want to change. Please remove, commas 123.45 also."

var repl = str.replace(/([a-z]+)\b[.,]/ig, '$1');

//=> "This is a sentence that I want to change Please remove commas 123.45 also"

([a-z]+)\b[.,] will match DOT or comma after a word that contains english letters thus leaving 123.45 unchanged.