String to slug

JavaScript, String, Regexp · Oct 4, 2020

Converts a string to a URL-friendly slug.

const slugify = str =>
  str
    .toLowerCase()
    .trim()
    .replace(/[^\w\s-]/g, '')
    .replace(/[\s_-]+/g, '-')
    .replace(/^-+|-+$/g, '');
slugify('Hello World!'); // 'hello-world'

More like this

  • HSL to object

    Converts an hsl() color string to an object with the values of each color.

    JavaScript, String · Oct 22, 2020

  • RGB to object

    Converts an rgb() color string to an object with the values of each color.

    JavaScript, String · Oct 22, 2020

  • Snakecase string

    Converts a string to snake case.

    JavaScript, String · Jun 28, 2021