去除字符串中空格

去除字符串前后的空格

function trim(str) {
return str.replace(/(^\s+)|(\s+$)/g, "");
}

去除字符串中所有空格

function removeAllSpace(str) {
return str.replace(/\s+/g, "");
}

用法举例:
alert(trim(' ab cd ')); //结果: ab cd
alert(removeAllSpace(' ab cd ')); //结果: abcd

posted @ 2017-07-05 14:39  余生莫度  阅读(313)  评论(0编辑  收藏  举报