在javascript中删除字符串内的空格 [英] Remove whitespaces inside a string in javascript

查看:93
本文介绍了在javascript中删除字符串内的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读关于javascript修剪的这个问题 ,然后用正则表达式的答案。

然后我期望修剪移除Hello和World之间的 inner 空间。

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

EDITED

为什么我期望!?

无意义!很显然,修剪并不会去除内部空间!只有前导和尾随,这是修剪的工作原理,然而这是一个非常错误的问题,我的歉意。

解决方案

,);

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


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

Then I expect trim to remove the inner space between Hello and 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.

解决方案

For space-character removal use

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

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

这篇关于在javascript中删除字符串内的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆