Skip to content

Instantly share code, notes, and snippets.

@eek
Last active August 4, 2021 14:23
Show Gist options
  • Save eek/9c4887e80b3ede05c0e39fee4dce3747 to your computer and use it in GitHub Desktop.
Save eek/9c4887e80b3ede05c0e39fee4dce3747 to your computer and use it in GitHub Desktop.
Vanilla JavaScript Slugify + Accent removal - Just another JavaScript Slugifier with an extra line for Accent Removal
function slugify(text) {
return text.toString().toLowerCase().trim()
.normalize('NFD') // separate accent from letter
.replace(/[\u0300-\u036f]/g, '') // remove all separated accents
.replace(/\s+/g, '-') // replace spaces with -
.replace(/&/g, '-and-') // replace & with 'and'
.replace(/[^\w\-]+/g, '') // remove all non-word chars
.replace(/--+/g, '-') // replace multiple '-' with single '-'
}
@rowild
Copy link

rowild commented Aug 4, 2021

Finally it made click and I believe to understand, what NFD does! Thank you very much for your efforts, @eek ! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment