Convert a string to URL slug
JavaScript version
const slugify = (str) =>
str
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '');
TypeScript version
const slugify = (str: string): string =>
str
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '');
Examples
slugify('Chapter One: Once upon a time...'); // 'chapter-one-once-upon-a-time'