Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JAVASCRIPT:去除字符串两边空格trim
<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>
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 18, 2017

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment