Add trim function to Javascript String Object


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



Copy this code and paste it in your HTML
  1. String.prototype.ltrim = function() {
  2. return this.replace(/^\s+/, '');
  3. };
  4. String.prototype.rtrim = function() {
  5. return this.replace(/\s+$/, '');
  6. };
  7.  
  8. String.prototype.trim = function() {
  9. return this.ltrim().rtrim();
  10. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.