Javascript String slugify()

Description

Javascript String slugify()

String.prototype.slugify = function() {
    var string = this.replace(/[^\w\s-]/g, '').trim().toLowerCase();
    return string.replace(/[_\s]+/g, '_');
};

Javascript String slugify()

/**/*from w  w  w .  j  a v  a 2s.  co  m*/
 * Taken from https://gist.github.com/mathewbyrne/1280286
 *
 * @returns {string}
 */
String.prototype.slugify = function() {
  return this.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
};



PreviousNext

Related