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

Enhanced JavaScript trim function code_javascript tips

WBOY
Release: 2016-05-16 19:10:26
Original
1020 people have browsed it
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!
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template