프로그램/Javascript

trim(), ltrim(), rtrim()

로드러너 2014. 3. 5. 21:44

Example

     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 str = “  123 ABC  ”;

 

     str.trim()     // “123 ABC”

     str.ltrim()     // “123 ABC  ”

     str.rtrim()     // “  123 ABC”