Posted By

enchance on 09/06/09


Tagged

whitespace remove spaces


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

guido


Removes unnecessary whitespace


 / Published in: JavaScript
 

  1. String.prototype.trim = function() {
  2. return this.replace(/^\s+|\s+$/g,"");
  3. }
  4. String.prototype.ltrim = function() {
  5. return this.replace(/^\s+/g,"");
  6. }
  7. String.prototype.rtrim = function() {
  8. return this.replace(/\s+$/g,"");
  9. }
  10.  
  11.  
  12. // Usage:
  13. var test = " Test ";
  14. var test1 = test.ltrim(); // returns "Test "
  15. var test2 = test.rtrim(); // returns " Test"
  16. var test3 = test.trim(); // returns "Test"

Report this snippet  

You need to login to post a comment.