Home > php教程 > php手册 > javascript trim方法

javascript trim方法

WBOY
Release: 2016-06-06 20:01:17
Original
1196 people have browsed it

在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,'');  

}  

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template