Skip to content

Instantly share code, notes, and snippets.

@shingchi
Forked from binjoo/gist:3971310
Created December 9, 2013 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shingchi/7883167 to your computer and use it in GitHub Desktop.
Save shingchi/7883167 to your computer and use it in GitHub Desktop.
<script language="javascript">
/*
* 写成类的方法格式如下:str.trim()
*/
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,"");
}
/*
* 写成函数可以这样:trim(str)
*/
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment