Javascript - 如何删除单词之间的所有额外间距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7635952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - How to remove all extra spacing between words
提问by P.Brian.Mackey
How can I remove all extra space between words in a string literal?
如何删除字符串文字中单词之间的所有额外空格?
"some value"
Should become
应该成为
"some value"
Also,
还,
" This should become something else too . "
Becomes
成为
"This should become something else too ."
Do not worry about moving the .
. Just as above is fine. I know I can use $.trim(str)
to achieve the trailing/ending space removal. But, I'm not sure how to do the 1 space between words trick.
不用担心搬家了.
。就像上面一样没问题。我知道我可以$.trim(str)
用来实现尾随/结尾空格的删除。但是,我不确定如何在单词之间做 1 个空格的技巧。
回答by Rob W
var string = " This should become something else too . ";
string = string.replace(/\s+/g, " ");
This code replaces a consecutive set of whitespace characters (\s+
) by a single white space. Note that a white-space character also includes tab and newlines. Replace \s
by a space if you only want to replace spaces.
此代码用\s+
单个空格替换一组连续的空格字符 ( )。请注意,空白字符还包括制表符和换行符。更换\s
,如果你只是想替换空格一个空格。
If you also want to remove the whitespace at the beginning and end, include:
如果您还想删除开头和结尾的空格,请包括:
string = string.replace(/^\s+|\s+$/g, "");
This line removes all white-space characters at the beginning (^
) and end ($
). The g
at the end of the RegExp means: global, ie match and replace all occurences.
此行删除开头 ( ^
) 和结尾 ( $
)处的所有空白字符。将g
在正则表达式来结束:全球性的,即匹配和替换所有出现。
回答by James Hill
var str = " This should become something else too . ";
str = str.replace(/ +(?= )/g,'');
Here's a working fiddle.
这是一个工作小提琴。
回答by Narendra Yadala
var str = " This should become something else too . "
$.trim(str).replace(/\s(?=\s)/g,'')
This uses lookahead to replace multiple spaces with a single space.
这使用前瞻用单个空格替换多个空格。
回答by used2could
回答by V-SHY
In case we want to avoid the replace function with regex,
如果我们想避免使用正则表达式替换函数,
We can achieve same result by
我们可以通过以下方式获得相同的结果
str.split(' ').filter(s => s).join(' ')
// var str = " This should become something else too . ";
// result is "This should become something else too ."
First, split the original string with space, then we will have empty string and words in an array. Second, filter to remain only words, then join all words with a whitespace.
首先,用空格分割原始字符串,然后我们将在数组中包含空字符串和单词。其次,过滤以仅保留单词,然后用空格连接所有单词。
回答by Matt Ball
Another (perhaps easier to understand) regexp replacement that will do the trick:
另一个(也许更容易理解)正则表达式替换可以解决问题:
var input = /* whatever */;
input = input.replace(/ +/g, ' ');
The regexp matches one or more spaces, so the .replace()
call replaces every single or repeated space with a single space.
正则表达式匹配一个或多个空格,因此.replace()
调用将每个单个或重复的空格替换为一个空格。
回答by Ivan
var str = 'some value';
str.replace(/\s\s+/g, ' ');