问题
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, "")
回答1:
That should work:
str = str.replace(/\s*,\s*/g, ",");
回答2:
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, ',')
);
回答3:
Simply use RegEx:
\s*,\s*
DEMO
回答4:
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
来源:https://stackoverflow.com/questions/39704104/remove-white-spaces-from-string-between-comma-and-any-letter