如何在 JavaScript 中删除字符串中的多余空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19155566/
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
How can I remove extra white space in a string in JavaScript?
提问by ankur
How can I remove extra white space (i.e. more than one white space character in a row) from text in JavaScript?
如何从 JavaScript 中的文本中删除多余的空格(即一行中的多个空格字符)?
E.g
例如
match the start using.
How can I remove all but one of the spaces between "match" and "the"?
如何删除“match”和“the”之间的除一个空格之外的所有空格?
回答by Rajesh
Use regex. Example code below:
使用正则表达式。示例代码如下:
var string = 'match the start using. Remove the extra space between match and the';
string = string.replace(/\s{2,}/g, ' ');
For better performance, use below regex:
为了获得更好的性能,请使用以下正则表达式:
string = string.replace(/ +/g, ' ');
Profiling with firebug resulted in following:
使用 firebug 进行分析的结果如下:
str.replace(/ +/g, ' ') -> 790ms
str.replace(/ +/g, ' ') -> 380ms
str.replace(/ {2,}/g, ' ') -> 470ms
str.replace(/\s\s+/g, ' ') -> 390ms
str.replace(/ +(?= )/g, ' ') -> 3250ms
回答by omgmog
See string.replaceon MDN
参见MDN 上的string.replace
You can do something like this:
你可以这样做:
var string = "Multiple spaces between words";
string = string.replace(/\s+/,' ', g);
回答by Adarsh Kumar
function RemoveExtraSpace(value)
{
return value.replace(/\s+/g,' ');
}
回答by Waqas Idrees
myString = Regex.Replace(myString, @"\s+", " ");
or even:
甚至:
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
tempo = regex.Replace(tempo, @" ");
回答by Ashok
Using regular expression.
使用正则表达式。
var string = "match the start using. Remove the extra space between match and the";
string = string.replace(/\s+/g, " ");
Here is jsfiddle for this
回答by jonathana
This can be done also with javascript logic.
here is a reusable function I wrote for that task.
LIVE DEMO
这也可以使用 javascript 逻辑来完成。
这是我为该任务编写的可重用函数。
现场演示
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>result:
<span id="spn">
</span>
</div>
<input type="button" value="click me" onClick="ClearWhiteSpace('match the start using. JAVASCRIPT CAN BE VERY FUN')"/>
<script>
function ClearWhiteSpace(text) {
var result = "";
var newrow = false;
for (var i = 0; i < text.length; i++) {
if (text[i] === "\n") {
result += text[i];
// add the new line
newrow = true;
}
else if (newrow == true && text[i] == " ") {
// do nothing
}
else if (text[i - 1] == " " && text[i] == " " && newrow == false) {
// do nothing
}
else {
newrow = false;
if (text[i + 1] === "\n" && text[i] == " ") {
// do nothing it is a space before a new line
}
else {
result += text[i];
}
}
}
alert(result);
document.getElementById("spn").innerHTML = result;
return result;
}
</script>
</body>
</html>
回答by Ajith S
Just do,
做就是了,
var str = "match the start using. Remove the extra space between match and the";
str = str.replace( /\s\s+/g, ' ' );
回答by Jamie Hutber
Sure, using a regex:
当然,使用正则表达式:
var str = "match the start using. Remove the extra space between match and the";
str = str.replace(/\s/g, ' ')