js、javascript去掉先後空格

function String.prototype.Trim() { return this.replace(/(^/s*)|(/s*$)/g, ""); }   // 去掉左右空格

function String.prototype.Ltrim() { return this.replace(/(^/s*)/g, ""); }            // 去掉左空格

function String.prototype.Rtrim() { return this.replace(/(/s*$)/g, ""); }            // 去掉右空格

<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>

轉載自:http://www.9958.pw/post/js_trimjavascript

相關文章
相關標籤/搜索