String.prototype.toLowerCase()
and String.prototype.trim()
to normalize the string.String.prototype.replace()
to replace spaces, dashes and underscores with -
and remove special characters.const slugify = str => str .toLowerCase() .trim() .replace(/[^\w\s-]/g, '') .replace(/[\s_-]+/g, '-') .replace(/^-+|-+$/g, '');
slugify('Hello World!'); // 'hello-world'
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️