Skip to content

Instantly share code, notes, and snippets.

@arturotena
Last active January 7, 2021 15:07
Show Gist options
  • Select an option

  • Save arturotena/22158dcabe386bbfc536 to your computer and use it in GitHub Desktop.

Select an option

Save arturotena/22158dcabe386bbfc536 to your computer and use it in GitHub Desktop.
JavaScript trim() polyfill
/* trim ployfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim */
if (!String.prototype.trim) {
(function() {
// Make sure we trim BOM and NBSP
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
String.prototype.trim = function() {
return this.replace(rtrim, '');
};
})();
}
/* Because old browsers (IE 8-) don't define trim() */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment