Files
30-seconds-of-code/snippets/slugify.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

619 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
String to slug string,regexp intermediate 2020-10-04T09:45:43+03:00 2020-10-04T10:36:38+03:00

Converts a string to a URL-friendly slug.

  • Use String.prototype.toLowerCase() and String.prototype.trim() to normalize the string.
  • Use 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'