TypeScript

Slugify (Unicode-aware)

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Turn any string into a URL-safe slug. Uses Intl normalize + diacritic stripping so "Café" becomes "cafe". No external dependency.
TypeScript
Raw
export function slugify(text: string, sep = '-'): string {
  return text
    .normalize('NFKD')                    // separate base chars + accents
    .replace(/[̀-ͯ]/g, '')      // strip combining marks
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, sep)          // non-alnum → sep
    .replace(new RegExp(`^${sep}+|${sep}+$`, 'g'), '')   // trim
    .replace(new RegExp(`${sep}{2,}`, 'g'), sep);        // collapse runs
}

slugify('Hello, World!');                  // 'hello-world'
slugify('Café au lait — délicieux');       // 'cafe-au-lait-delicieux'
slugify('TypeScript 5.3 & Beyond');        // 'typescript-5-3-beyond'
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.