Home  >  Article  >  php教程  >  javascript trim方法

javascript trim方法

WBOY
WBOYOriginal
2016-06-06 20:01:171161browse

在javascript中的string对象没有trim方法,所以trim功能需要自己实现: 代码如下: scriptlanguage="javascript" /** *删除左右两端的空格 */ String.prototype.trim=function(){ return this.replace(/(^/s*)|(/s*$)/g, ''); } /** *删除左边的空格 */ Stri

 

在javascript中的string对象没有trim方法,所以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,'');  

}  

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:葛优Next article:JQuery 常用方法基础教程