Skip to content

Instantly share code, notes, and snippets.

@BenjaminAdams
Created March 27, 2013 21:40
Show Gist options
  • Save BenjaminAdams/5258272 to your computer and use it in GitHub Desktop.
Save BenjaminAdams/5258272 to your computer and use it in GitHub Desktop.
Problem, we need to trim strings in javascript but its not implemented in all browsers Since new Browsers (IE9+) have trim() already implemented, you should only implement trim() if it is not already available on the Prototype-Object (overriding it is a huge performance hit). This is generally recommended when extending Native Objects! Note that…
if (!String.prototype.trim) {
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment