如何删除字符串中的空格?例如:
输入:
'/var/www/site/Brand new document.docx'输出:
'/var/www/site/Brandnewdocument.docx'发布于 2011-05-11 11:06:00
发布于 2011-05-11 11:10:49
var a = b = " /var/www/site/Brand new   document.docx ";
console.log( a.split(' ').join('') );
console.log( b.replace( /\s/g, '') ); 
这有两种方法!
发布于 2018-07-13 09:23:35
最短最快的:str.replace(/ /g, '');
基准:
这里我的结果是:(2018.07.13) MacOs High塞拉利昂10.13.3 on Chrome 67.0.3396 (64位),Safari 11.0.3 (13604.5.6),Firefox 59.0.2 (64位):
短弦
类似于OP问题中的示例的短字符串

所有浏览器上最快的解决方案是/ /g (regexp1a) -Chrome17.7m(操作/秒),Safari10.1M,Firefox8.8M。对于所有浏览器来说,最慢的是split-join解决方案。更改为\s或添加+或i以恢复处理速度。
长弦
对于大约3毫安字符的字符串,结果是:
您可以在您的计算机上运行它:https://jsperf.com/remove-string-spaces/1
https://stackoverflow.com/questions/5963182
复制相似问题