Skip to content

Slugify a string in JavaScript

FULL-STACK WEB DEVELOPMENT BOOTCAMP

2024 COHORT SIGNUPS END TOMORROW

Install the slugify library

npm i slugify

Then import it

import slugify from 'slugify'

and you can do

const slug = slugify('Testing this')
console.log(slug) //testing-this

Note that if the string contains dots or other punctuation, it will not strip that.

To remove that, you can use a regex like this:

slugify('Testing. this!', { remove: /[*+~.,()'"!:@]/g })
  • Learn modern web development in my BOOTCAMP (SIGNUP END TOMORROW FEB 20, 2024)
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ coding BOOKS, all available in THE VALLEY OF CODE PRO
  • Indie solopreneur internet business masterclass SOLO LAB (summer 2024)