Javascript Slugify

Javascript function to “slugify” a string:

function slugify ( str ) {
  str   = str.toLowerCase();
  str   = str.replace( /\s/gi, "-" );
  str   = str.replace( /\//gi, "-" );
  str   = str.replace( /[^-a-zA-Z0-9,&/\\s]+/ig, '' );
  
  // Trim dash from beginning, end.
  while( str.substr( 0, 1 ) == '-' )  str   = str.substr( 1 );
  while( str.substr( -1 ) == '-' )    str   = str.substr( 0, str.length -1 );
  
  return str;
}