Skip to content

Instantly share code, notes, and snippets.

@ryansukale
Created March 27, 2014 18:49
Show Gist options
  • Save ryansukale/9815158 to your computer and use it in GitHub Desktop.
Save ryansukale/9815158 to your computer and use it in GitHub Desktop.
Trimming a string in javascript
//Reference
//http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript
if (!String.prototype.trim) {
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim=function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim=function(){return this.replace(/\s+$/,'');};
String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment