1、trim方法:过滤字符串两端的空格
var str=" 6 66 "; console.log(str.trim()); //6 66
trim() 是最常用的方法,也是最方便的方法。
2、replace方法(正则表达式)
let str = ' xin xiang '; console.log('去掉左边空格'); console.log(str.replace(/^\s*/g, '')); //xin xiang console.log('去掉右边空格'); console.log(str.replace(/\s*$/g, '')); // xin xiang console.log('去掉中间空格'); console.log(str.replace(/[ ]/g,'')); // xinxiang console.log('去掉两端空格'); console.log(str.replace(/^\s*|\s*$/g, '')); //xin xiang console.log('去掉全部端空格'); console.log(str.replace(/\s+/g, '')); //xinxiang
标签:console,log,js,空格,str,字符串,去掉,replace From: https://www.cnblogs.com/heibaiqi/p/16626546.html