去除字符串前后的空格function trim(str) {return str.replace(/(^\s+)|(\s+$)/g, "");}去除字符串中所有空格function removeAllSpace(str) {return str.replace(/\s+/g, "");}用法举例:alert(trim(' ab cd ')); //结果: ab cdalert(removeAllSpace(' ab cd ')); //结果: abcd