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

输入:

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

输出:

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

当前回答

简单的方法

someString.replace(/ /g, '');
// or
someString.replace(/\s/gm, '');

其他回答

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

有两种方法!

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

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

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

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

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

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

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

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

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

alert(input);

点击这里查看工作示例