如何删除字符串中的空格?例如:

输入:

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

输出:

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

当前回答

如果没有regexp,它只适用于一种情况。

input = input.replace(' ', '');

这是更快的简单! 在某些情况下能帮到你们中的一些人。

其他回答

从字符串中删除空格最简单的方法是使用replace

let str = '/var/www/site/Brand new document.docx';
let result = str.replace(/\s/g, '');

你可以使用regex从字符串中移除空格

let str = '/var/www/site/Brand new document.docx';
let result = str.replace(/\s/g, '');

var a = b = " /var/www/site/Brand new document.docx "; Console.log (a.split(' ').join(")); Console.log (b.replace(/\s/g, "));

有两种方法!

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

注意:虽然你使用'g'或'gi'来删除空格,但它们的行为是一样的。

如果我们在replace函数中使用'g',它将检查完全匹配。但如果我们使用'gi',它就忽略了大小写敏感性。

参考请点击这里。

var str='/var/www/site/全新文档.docx'; document。写入(str.replace(/\ s/g,"); ----------