去除字符串空格
Sunny-117 opened this issue · comments
var str = ' as '
//方法1
str = str.replace(/\s*/g,"");//去除所有空格
str = str.replace(/^\s*|\s*$/g,"");//去除两头空格
str = str.replace(/^\s*/,"");//去除左边空
str = str.replace(/(\s*$)/g,"");//去除右边空格
//方法2:trim()方法:只能去除两边空格
function trim(str) {
if (str && typeof str === "string") {
return str.replace(/(^\s*)|(\s*)$/g, "");//去除前后空白符
}
}
//方法3 jquery $.trim(str)