本文翻译自: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"