现在的位置: 首页 > 综合 > 正文

js去除空格函数

2013年04月17日 ⁄ 综合 ⁄ 共 543字 ⁄ 字号 评论关闭

 <script language="javascript">
  String.prototype.trim=function(){
   return this.replace(/(^\s*)|(\s*$)/g, "");
  }
  String.prototype.ltrim=function(){
   return this.replace(/(^\s*)/g,"");
  }
  String.prototype.rtrim=function(){
   return this.replace(/(\s*$)/g,"");
  }
  </script>
  写成函数可以这样:(trim(str))
  <script type="text/javascript">
  function trim(str){ //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
  }
  function ltrim(str){ //删除左边的空格
  return str.replace(/(^\s*)/g,"");
  }
  function rtrim(str){ //删除右边的空格
  return str.replace(/(\s*$)/g,"");
  }
  </script>

抱歉!评论已关闭.