//去左空格;
function ltrim(s){
return s.replace( /^\s*/, "");
}
//去右空格;
function rtrim(s){
return s.replace( /\s*$/, "");
}
//左右空格;
function trim(s){
return rtrim(ltrim(s));
}
如果是去掉半角和全角空格就把 \s 替换成 [" "|" "] 就变成
//去左空格;
function ltrim(s){
return s.replace( /^[" "|" "]*/, "");
}
//去右空格;
function rtrim(s){
return s.replace( /[" "|" "]*$/, "");
}
//左右空格;
function trim(s){
return rtrim(ltrim(s));
}
去除所有空格:
str = str.replace(/\s+/g,"");
去除两头空格:
str = str.replace(/^\s+|\s+$/g,"");