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