Removes unnecessary whitespace


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.