Home >Web Front-end >JS Tutorial >Enhanced JavaScript trim function code_javascript tips

Enhanced JavaScript trim function code_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:10:261045browse
Copy code The code is as follows:

String.prototype.trim=function(){
var _argument = arguments[0] || " ";
var _re= new RegExp("(^" _argument "*)|(" _argument "*$)","g"); // Case sensitive
return this.replace(_re,"");
}
String.prototype.ltrim=function(){
var _argument = arguments[0] || " ";
var _re= new RegExp("(^" _argument "*)","g");
return this.replace(_re,"");
}
String.prototype.rtrim=function(){
var _argument = arguments[0] || " ";
var _re= new RegExp("(" _argument "*$)","g");
return this.replace(_re,"") ;
}



string.trim(",") means to delete the "," on the left and right ends of string. If trim takes no parameters, the default is to delete both spaces at the end.
More powerful than the previous trim!
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