Trim for jQuery

trim() is another function I miss in jQuery / Javascript, the below function was found in a random stackoverflow thread but works great

This trims whitespace from the start and end of a string

(function () {
 if (!String.prototype.trim) {
 /**
  * Trim whitespace from each end of a String
  * @returns {String} the original String with whitespace removed from each end
  * @example
  * ' foo bar    '.trim(); //'foo bar'
  */
 String.prototype.trim = function trim() {
 return this.toString().replace(/^([\s]*)|([\s]*)$/g, '');
 };
 }
 })();

Justin Kelly

Justin Kelly

Web Developer, Business Analytics, Data Engineer specialising in PHP, ReactJS, Tableau, AWS RedShift and Matillion ETL

Based in Melbourne, Australia

Feel free to contact me justin@kelly.org.au or _justin_kelly

Leave a comment

Name Notify me of replies by email.