hidjou

Slugify function

Dec 13th, 2018
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const slugify = function(text) {
  2.   return text
  3.     .toString()
  4.     .toLowerCase()
  5.     .replace(/\s+/g, '-')       // Replace spaces with -
  6.     .replace(/[^\w-]+/g, '')    // Remove all non-word chars
  7.     .replace(/--+/g, '-')       // Replace multiple - with single -
  8.     .replace(/^-+/, '')         // Trim - from start of text
  9.     .replace(/-+$/, '')         // Trim - from end of text
  10. }
Advertisement
Add Comment
Please, Sign In to add comment