在javascript中添加自定义trim()方法去除字符串两边空格
来源:原创
时间:2015-08-25
作者:脚本小站
分类:JS/JQuery
在php中trim方法很常用也很实用,但在javascript中却没有提供该方法,只能直接写一个。
只要在js中加入如下代码即可调用自定义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,""); } var string = ‘ hello ’; alert(string.trim());