function slugify(str) {
return str
.normalize('NFD') // decompose accented chars
.replace(/[̀-ͯ]/g, '') // strip accent marks
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '') // remove non-alphanumeric
.replace(/[\s]+/g, '-') // spaces → hyphens
.replace(/-+/g, '-'); // collapse multiple hyphens
}
// Usage
console.log(slugify('Hello World!')); // hello-world
console.log(slugify(' Über Café ')); // uber-cafe
console.log(slugify('10 Reasons to Use JS')); // 10-reasons-to-use-js
Create a free account and build your private vault. Share publicly whenever you want.