Javascript String truncate(len = 50)
/**//w w w . jav a2s .co m * Truncate String * truncate string after `len` characters * @param {number} [len = 50] len */ String.prototype.truncate = function(len = 50){ var regx = new RegExp('^.{0,'+ len +'}[\S]*') var matches_array = this.match(regx); var str_length = matches_array[0].length; var replacement = matches_array[0].replace(/\s$/,''); return ((this.length > str_length ) ? (replacement + "..."): (replacement) ) }
/**//from w w w . j a va2s . c om * Truncate String * truncate string after `len` characters * @param {number} [len = 50] len */ String.prototype.truncate = function(len = 50){ var regx = new RegExp('^.{0,'+ len +'}[\S]*') var matches_array = this.match(regx); var str_length = matches_array[0].length; var replacement = matches_array[0].replace(/\s$/,''); return ((str_length > this.length) ? (replacement) : (replacement + "...") ) }