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

时间:2021-09-23 21:41:38

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?

我使用了trim函数来删除句子开头和结尾的空格。如果句子中的单词之间有很多空格,是否有任何方法可以修剪?

for example

例如

"abc                  def. fds                            sdff."

6 个解决方案

#1


25  

try

尝试

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

or

要么

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

or use this allTrim String extension

或使用此allTrim String扩展

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?"

#2


24  

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

#3


3  

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."

#4


2  

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.

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

#5


1  

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, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

#6


1  

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个空格。

#1


25  

try

尝试

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

or

要么

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

or use this allTrim String extension

或使用此allTrim String扩展

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?"

#2


24  

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

#3


3  

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."

#4


2  

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.

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

#5


1  

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, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

#6


1  

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个空格。