Skip to content

Instantly share code, notes, and snippets.

@solrevdev
Created July 7, 2023 14:00
Show Gist options
  • Save solrevdev/91f05dd41e2d9674a080f926ec8155a7 to your computer and use it in GitHub Desktop.
Save solrevdev/91f05dd41e2d9674a080f926ec8155a7 to your computer and use it in GitHub Desktop.
vanilla javascript method to slugify a string
function slugify(input) {
if (!input)
return '';
// make lower case and trim
var slug = input.toLowerCase().trim();
// remove accents from charaters
slug = slug.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
// replace invalid chars with spaces
slug = slug.replace(/[^a-z0-9\s-]/g, ' ').trim();
// replace multiple spaces or hyphens with a single hyphen
slug = slug.replace(/[\s-]+/g, '-');
return slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment