js字符串去空格的方法
1.使用trim()方法:该方法可以删除字符串两端的空白字符,包括空格、制表符、换行符等。如下所示:
let str = ' hello world ';
let newStr = im();
console.log(newStr); // 'hello world'
2. 使用replace()方法:该方法可以使用正则表达式匹配所有空白字符,并将其替换为空字符串。如下所示:
let str = ' hello world ';
let newStr = place(/s+/g, '');
console.log(newStr); // 'helloworld'js 正则替换
3. 使用正则表达式:可以使用正则表达式匹配所有空白字符,并删除它们。如下所示:
let str = ' hello world ';
let newStr = place(/^s+|s+$/g, '');
console.log(newStr); // 'hello world'
以上就是js字符串去空格的方法,可以根据具体需求选择不同的方法。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论