javascript 清除字符串空格

来源:互联网 发布:双代号网络计划规则 编辑:程序博客网 时间:2024/05/22 17:35

去除字符串前后的空格

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
0 0
原创粉丝点击