Posted By

riddla on 06/29/10


Tagged

javascript String slug slugify


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

riddla


Slugify - JavaScript version


 / Published in: JavaScript
 

  1. function slugify(s)
  2. {
  3. //s = 'Was wäre daß® für ein + unnützer Tést?';
  4.  
  5. var slug = s;
  6.  
  7. slug = slug.toLowerCase();
  8. slug = slug.replace(/\s+/g,'-');
  9.  
  10. tr = {
  11. '\u00e4':'ae',
  12. '\u00fc':'ue',
  13. '\u00f6':'oe',
  14. '\u00df':'ss',
  15. '\u00df':'ss',
  16. '\u00e9':'e',
  17. '/':'-'
  18. }
  19.  
  20. for ( var key in tr )
  21. {
  22. slug = slug.replace(new RegExp(key, 'g'), tr[key]);
  23. }
  24.  
  25. slug = slug.replace(/[^a-zA-Z0-9\-]/g,'');
  26. slug = slug.replace(/-+/g, '-');
  27.  
  28. //alert(slug);
  29. // return(s);
  30.  
  31. return slug;
  32. }

Report this snippet  

You need to login to post a comment.