How to Slugify a String in JavaScript (with Code)
A practical guide to converting any string into a clean URL slug in JavaScript — handling accents, special characters, and edge cases, with copy-paste code.

Turning a title into a URL slug is one of those tasks that looks trivial until you hit the edge cases: accented letters, emoji, double spaces, leading hyphens. This guide walks through a robust slugify function in JavaScript, building it up step by step.
Just need a slug right now? Paste your text into the URL slug generator — no code required. Read on if you want to build it yourself.
The naive version (and why it breaks)
The simplest possible slugify looks like this:
function slugify(text) {
return text.toLowerCase().replace(/\s+/g, '-')
}
slugify('Hello World') // "hello-world"
It works for clean English input, but it falls apart fast:
slugify('Café & Crème!') // "café-&-crème!" ❌ keeps accents and symbols
slugify(' Spaced Out ') // "-spaced--out-" ❌ stray and doubled hyphens
We need to handle accents, strip unwanted characters, and clean up the edges.
A robust slugify function
Here's a version that handles the common cases:
function slugify(text) {
return text
.toString()
.normalize('NFD') // split accented letters into base + mark
.replace(/[̀-ͯ]/g, '') // remove the diacritical marks
.toLowerCase()
.trim()
.replace(/[^a-z0-9\s-]/g, '') // strip anything that isn't a letter, number, space, or hyphen
.replace(/\s+/g, '-') // spaces → hyphens
.replace(/-+/g, '-') // collapse multiple hyphens
.replace(/^-+|-+$/g, '') // trim hyphens from the ends
}
Let's test it:
slugify('Café & Crème!') // "cafe-creme"
slugify(' The Ultimate Guide to SEO ') // "the-ultimate-guide-to-seo"
slugify('Diseño Web en España') // "diseno-web-en-espana"
Much better.
How each step works
normalize('NFD')decomposes characters likeéintoe+ a combining accent mark.replace(/[̀-ͯ]/g, '')deletes those combining marks, leaving the base letter (e).toLowerCase()enforces lowercase URLs.replace(/[^a-z0-9\s-]/g, '')removes punctuation, symbols, and emoji.- The hyphen steps convert spaces, collapse repeats, and trim the ends so you never get
--or a leading-.
Adding optional stop-word removal
To drop stop words, filter them out before joining:
const STOP_WORDS = new Set(['a', 'an', 'the', 'and', 'or', 'of', 'to', 'in', 'on', 'for', 'with'])
function slugify(text, { removeStopWords = false } = {}) {
let words = text
.normalize('NFD')
.replace(/[̀-ͯ]/g, '')
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, '')
.trim()
.split(/[\s-]+/)
.filter(Boolean)
if (removeStopWords) {
words = words.filter(word => !STOP_WORDS.has(word))
}
return words.join('-')
}
slugify('The Best Tips for Writing a Blog Post', { removeStopWords: true })
// "best-tips-writing-blog-post"
The NFD blind spot: letters that don't decompose
Our robust function has a subtle bug that most tutorials share. normalize('NFD') only helps with letters built from a base character plus an accent. Several common letters are standalone characters, so the [^a-z0-9\s-] filter deletes them outright:
slugify('Straße') // "strae" ❌ should be "strasse"
slugify('Łódź Guide') // "odz-guide" ❌ should be "lodz-guide"
slugify('Ørsted') // "rsted" ❌ should be "orsted"
slugify("Don't stop") // "dont-stop" — fine here, but a version that
// replaces symbols with spaces gives "don-t-stop"
The fix is a small transliteration map applied after NFD, plus explicit handling of & and apostrophes:
const SPECIALS = {
ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', ł: 'l', đ: 'd', ð: 'd', þ: 'th', ı: 'i'
}
function slugify(text) {
return text
.toString()
.replace(/&/g, ' and ')
.replace(/['’]/g, '') // don't → dont
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.replace(/[ßæœøłđðþı]/g, ch => SPECIALS[ch])
.replace(/[^a-z0-9\s-]/g, ' ')
.trim()
.replace(/[\s-]+/g, '-')
}
slugify('Straße & Łódź: Don’t Miss!') // "strasse-and-lodz-dont-miss"
This is the same approach the Slugme.io engine uses, extended with Cyrillic and Greek tables and German-specific umlaut rules (ä → ae).
Using a library instead
If you would rather not maintain character tables, two npm packages are solid choices:
// npm i slugify
import slugify from 'slugify'
slugify('Straße & Café', { lower: true, strict: true }) // "strasse-and-cafe"
// npm i @sindresorhus/slugify (ESM only)
import slugify from '@sindresorhus/slugify'
slugify('Straße & Café') // "strasse-and-cafe"
Note that slugify keeps the original case unless you pass lower: true. Our slugify libraries comparison covers both, plus Python, PHP, Ruby and Go equivalents.
The limits of the Latin-only approach
This function works for English, French, and Spanish because they use the Latin alphabet. But it deletes non-Latin scripts entirely:
slugify('أفضل نصائح السيو') // "" ❌ empty string
For Arabic and other non-Latin languages, you need a different strategy: preserve the script and strip only diacritics, rather than removing everything outside a–z. That's a meaningfully more complex problem — see URL slugs for non-Latin languages.
When to use a library or tool instead
Rolling your own is great for learning and for full control. But if you need to handle many languages, RTL text, and per-language stop words, a dedicated tool saves time and edge-case headaches. Slugme.io does all of this in the browser with language-aware engines for English, French, Spanish, German and Arabic — and you can see the rules it applies before you ship them in your own code.
The takeaway
A solid JavaScript slugify normalizes accents, lowercases, strips unwanted characters, and tidies the hyphens. Add stop-word filtering when you want shorter slugs. Just remember the Latin-only limitation — non-Latin scripts need dedicated handling.
Frequently asked questions
What does normalize("NFD") do in JavaScript?
It decomposes characters into a base letter plus combining marks — é becomes e + ◌́. Removing the combining-mark range (U+0300–U+036F) afterwards leaves the plain base letter.
Which npm package should I use to slugify strings?
For most projects, "slugify" (zero dependencies, works in CommonJS and ESM) or "@sindresorhus/slugify" (ESM-only, excellent transliteration, built-in "&" → "and"). See our slugify libraries comparison for details.
How do I make slugs unique in JavaScript?
Track slugs you have already produced and append a counter: my-post, my-post-2, my-post-3. @sindresorhus/slugify ships a slugifyWithCounter() helper for exactly this; for database records, enforce uniqueness with a unique index.