Need a Regular Expression that replaces all duplicate whitespace -next to each other- with a single whitepsace
I have a simple piece of script (which uses RegEx) which cleans up a source string to leave only alpha-numeric-and-whitespace characters.
Sometimes, I end up with a number of whitespace characters next to each other.
eg.
source: abc def ghi
result: abc def ghi
source: a*bc D*f
result: abc df
source: a*bc *** def
result: abc def <-- notice the two spaces in there
expected result: abc def <-- notice one space, here.
So i was hoping some regex could look for 2开发者_如何学JAVA+ spaces next to each other, in some source string and replace it with a single whitespace character.
cheers :)
Just use \s\s+
as the string to match, and a single space as the replacement.
In C# this would be:
Regex regex = new Regex("\\s\\s+");
string output = regex.Replace(input, " ");
Here's a quick JavaScript function. This does all whitespace, not just spaces.
function stripExtraSpaces(text)
{
var exp = new RegExp("[\\s]+","g");
return text.replace(exp," ");
}
精彩评论