欢迎来到我的博客文章!所有文章都是满满的前端干货,文章简明扼要。
将字符串按空格分割成数组,然后再拼接回去:
function removeSpaces(str) {
return str.split(" ").join("");
}
let str = "h e l l o w o r l d";
console.log(removeSpaces(str)); // Output: "helloworld" 使用正则表达式 /\s+/g 匹配所有空白字符:
function removeSpaces(str) {
return str.replace(/\s+/g, '');
}
let str = "h e l l o w o r l d";
console.log(removeSpaces(str)); // Output: "helloworld"