Javascript 在javascript中删除字符串内的空格

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

Remove whitespaces inside a string in javascript

javascripthtmltrim

提问by Hernán Eche

I've read this questionabout javascript trim, with a regex answer.

我已经阅读了有关 javascript trim 的问题,并附有正则表达式答案。

Then I expect trim to remove the innerspace between Hello and World.

然后我希望修剪去除Hello 和 World 之间的内部空间。

function myFunction() {
    alert("Hello World ".trim());
}

EDITED

已编辑

Why I expected that!?

为什么我这么期待!?

Nonsense! Obviously trim doesn't remove inner spaces!, only leading and trailing ones, that's how trim works, then this was a very wrong question, my apologies.

废话!显然修剪不会删除内部空间!,只有前导和尾随,这就是修剪的工作原理,那么这是一个非常错误的问题,我很抱歉。

回答by Henrik Andersson

For space-character removal use

对于空格字符删除使用

"hello world".replace(/\s/g, "");

for all white space use the suggestion by Rocket in the comments below!

对于所有空白,请在下面的评论中使用 Rocket 的建议!

回答by John Conde

Probably because you forgot to implement the solution in the accepted answer. That's the code that makes trim()work.

可能是因为您忘记在已接受的答案中实施解决方案。这就是trim()工作的代码。

update

更新

This answer only applies to older browsers. Newer browsers apparently support trim()natively.

此答案仅适用于较旧的浏览器。较新的浏览器显然支持trim()本机。

回答by JSON C11

You can use Strings replacemethod with a regular expression.

您可以使用带有正则表达式的字符串替换方法。

"Hello World ".replace(/ /g, "");

"Hello World ".replace(/ /g, "");

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp

replace() 方法返回一个新字符串,其中部分或所有模式匹配项被替换项替换。模式可以是字符串或正则表达式

RegExp

正则表达式

  • / /- Regular expression matching spaces

  • g- Global flag; find all matches rather than stopping after the first match

  • //- 正则表达式匹配空格

  • g- 全局标志;查找所有匹配项而不是在第一个匹配项后停止

const str = "H    e            l l       o  World! ".replace(/ /g, "");
document.getElementById("greeting").innerText = str;
<p id="greeting"><p>

回答by ArlanG

You can use

您可以使用

"Hello World ".replace(/\s+/g, '');

trim()only removes trailing spaces on the string (first and last on the chain). In this case this regExp is faster because you can remove one or more spaces at the same time.

trim()只删除字符串上的尾随空格(链上的第一个和最后一个)。在这种情况下,这个 regExp 更快,因为您可以同时删除一个或多个空格。

If you change the replacement empty string to '$', the difference becomes much clearer:

如果将替换空字符串更改为 '$',则差异变得更加清晰:

var string= '  Q  W E   R TY ';
console.log(string.replace(/\s/g, '$'));  // $$Q$$W$E$$$R$TY$
console.log(string.replace(/\s+/g, '#')); // $Q$W$E$R$TY$

Performance comparison - /\s+/gis faster. See here: http://jsperf.com/s-vs-s

性能比较 -/\s+/g更快。见这里:http: //jsperf.com/s-vs-s