如何使用JavaScript从字符串中删除空格?

本文翻译自:How to remove spaces from a string using JavaScript?

How to remove spaces in a string? 如何删除字符串中的空格? For instance: 例如:

Input: 输入:

'/var/www/site/Brand new document.docx'

Output: 输出:

'/var/www/site/Brandnewdocument.docx'

#1楼

参考:https://stackoom.com/question/P1IM/如何使用JavaScript从字符串中删除空格


#2楼

var input = '/var/www/site/Brand new document.docx';

//remove space
input = input.replace(/\s/g, '');

//make string lower
input = input.toLowerCase();

alert(input);

Click here for working example 点击此处查看工作示例


#3楼

  var output = '/var/www/site/Brand new document.docx'.replace(/ /g, ""); 
    or
  var output = '/var/www/site/Brand new document.docx'.replace(/ /gi,"");

Note: Though you use 'g' or 'gi' for removing spaces both behaves the same. 注意:虽然使用'g'或'gi'来删除空格,但两者的行为都相同。

If we use 'g' in the replace function, it will check for the exact match. 如果我们在替换函数中使用'g',它将检查完全匹配。 but if we use 'gi', it ignores the case sensitivity. 但如果我们使用'gi',它会忽略区分大小写。

for reference click here . 供参考点击这里


#4楼

Following @rsplak answer: actually, using split/join way is faster than using regexp. 关注@rsplak回答:实际上,使用split / join方式比使用regexp更快。 See the performance test case 查看性能测试用例

So 所以

var result = text.split(' ').join('')

operates faster than 运作速度比

var result = text.replace(/\\s+/g, '')

On small texts this is not relevant, but for cases when time is important, eg in text analisers, especially when interacting with users, that is important. 在小文本上,这是不相关的,但对于时间很重要的情况,例如在文本分析器中,特别是在与用户交互时,这很重要。


On the other hand, \\s+ handles wider variety of space characters. 另一方面, \\s+处理更多种类的空间字符。 Among with \\n and \\t , it also matches character, and that is what   \\n\\t ,它也匹配字符,这就是  is turned in, when getting text using textDomNode.nodeValue . 在使用textDomNode.nodeValue获取文本时textDomNode.nodeValue

So I think that conclusion in here can be made as follows: if you only need to replace spaces ' ' , use split/join. 所以我认为这里的结论可以如下:如果你只需要替换空格 ' ' ,请使用split / join。 If there can be different symbols of symbol class - use replace(/\\s+/g, '') 如果符号类可以有不同的符号 - 使用replace(/\\s+/g, '')


#5楼

SHORTEST and FASTEST : str.replace(/ /g, ''); 最短和最快str.replace str.replace(/ /g, '');


Benchmark: 基准测试:

Here my results - (2018.07.13) MacOs High Sierra 10.13.3 on Chrome 67.0.3396 (64-bit), Safari 11.0.3 (13604.5.6), Firefox 59.0.2 (64-bit) ): 在这里我的结果 - (2018.07.13)MacO High Sierra 10.13.3在Chrome 67.0.3396(64位),Safari 11.0.3(13604.5.6),Firefox 59.0.2(64位)):

SHORT strings SHORT字符串

Short string similar to examples from OP question 短字符串类似于OP问题的例子

在此输入图像描述

The fastest solution on all browsers is / /g (regexp1a) - Chrome 17.7M (operation/sec), Safari 10.1M, Firefox 8.8M. 所有浏览器上最快的解决方案是/ /g (regexp1a) - Chrome 17.7M(操作/秒),Safari 10.1M,Firefox 8.8M。 The slowest for all browsers was split-join solution. 所有浏览器中最慢的是split-join解决方案。 Change 更改 to \\s or add + or i to regexp slows down processing. \\s或添加+i到regexp减慢处理速度。

LONG strings 长串

For string about ~3 milion character results are: 对于大约3个百万字符串的字符串结果是:

  • regexp1a : Safari 50.14 ops/sec, Firefox 18.57, Chrome 8.95 regexp1a :Safari 50.14 ops / sec,Firefox 18.57,Chrome 8.95
  • regexp2b : Safari 38.39, Firefox 19.45, Chrome 9.26 regexp2b :Safari 38.39,Firefox 19.45,Chrome 9.26
  • split-join : Firefox 26.41, Safari 23.10, Chrome 7.98, split-join :Firefox 26.41,Safari 23.10,Chrome 7.98,

You can run it on your machine: https://jsperf.com/remove-string-spaces/1 您可以在您的计算机上运行它: https//jsperf.com/remove-string-spaces/1


#6楼

Although there are ways to use regex to remove spaces, there is a simple function that can remove all whitespace called .trim(); 虽然有办法使用正则表达式来删除空格,但是有一个简单的函数可以删除所有名为.trim();空格.trim(); :

var str = "abr a cadab ra";
str = str.trim();
//str = "abracadabra"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值