slugify

Convert a string to a URL-friendly slug.

#string #url #utility
export const slugify = (str: string) =>
str
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
// Usage
slugify("Hello World!"); // "hello-world"
slugify("Café résumé"); // "cafe-resume"
slugify(" Spaces Around "); // "spaces-around"

Share this snippet

Comments