Javascript 如何删除字符串中的重复空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7764319/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:31:32  来源:igfitidea点击:

How to remove duplicate white spaces in a string?

javascriptregex

提问by Lakshmitha

Possible Duplicate:
Removing whitespace from string in JavaScript

可能的重复:
从 JavaScript 中的字符串中删除空格

I have used trim function to remove the white spaces at the beginning and end of the sentences. If there is to much white spaces in between words in a sentence, is there any method to trim?

我已经使用修剪功能去除了句子开头和结尾的空格。如果句子中的单词之间有很多空格,有什么方法可以修剪吗?

for example

例如

"abc                  def. fds                            sdff."

回答by KooiInc

try

尝试

"abc                  def. fds                            sdff."
 .replace(/\s+/g,' ')

or

或者

"abc                  def. fds                            sdff."
     .split(/\s+/)
     .join(' ');

or use this allTrimString extension

或使用此allTrim字符串扩展名

String.prototype.allTrim = String.prototype.allTrim ||
     function(){
        return this.replace(/\s+/g,' ')
                   .replace(/^\s+|\s+$/,'');
     };
//usage:
alert(' too much whitespace     here   right?  '.allTrim());
   //=> "too much whitespace here right?"

回答by Shef

var str = 'asdads   adasd    adsd';
str = str.replace(/\s+/g, ' ');

回答by jfriend00

You can trim multiple consecutive spaces down to only one space with this Javascript:

您可以使用此 Javascript 将多个连续空格修剪为仅一个空格:

var str = "abc def.  fds    sdff.";
str = str.replace(/\s+/g, " ");

If you meant something other than multiple consecutive spaces, then please clarify in your question what you meant.

如果您的意思不是多个连续空格,那么请在您的问题中澄清您的意思。

回答by Ehryk

I think what you're looking for is JavaScript's string.replace() method.

我认为您正在寻找的是 JavaScript 的 string.replace() 方法。

If you want all whitespace removed, use this:

如果要删除所有空格,请使用以下命令:

"abc def. fds sdff.".replace(/\s/g, '');
Returns: "abcdef.fdssdff."

If you want only double-spaces removed, use:

如果您只想删除双空格,请使用:

"abc   def.  fds      sdff.".replace(/\s\s/g, ' ');
Returns: "abc def. fds sdff."

If you want the space left after a period, use:

如果您想在一段时间后留下空间,请使用:

"abc def. fds   sdff.".replace(/[^.]\s/g, '')
Returns: "abcdef. fdssdff."

回答by samn

value.replace("  ", " ");

This should do the trick, it just replaces 2 white spaces to one, until you get only 1 white space.

这应该可以解决问题,它只是将 2 个空格替换为 1,直到您只得到 1 个空格。

回答by Sai Kalyan Kumar Akshinthala

Refer to the following code part which used rejex in javascript to remove duplicate white spaces.

请参阅以下代码部分,该部分在 javascript 中使用 rejex 删除重复的空格。

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, ''); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}