Copyright 2025

The Valley of Code

A Flavio Copes project

Slugify a string in JavaScript
Slugify a string in JavaScript

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 })

Want to master TypeScript? Check out my TypeScript Masterclass

Join my AI Workshop !

The Web Development BOOTCAMP cohort starts in February 2026