A Tested JavaScript slugify() Function: Accents, Symbols and Edge Cases
Quick answer: normalise the text, strip accent marks, lowercase it, replace everything that is not a letter or digit with one hyphen, and trim the ends. The function below does that and handles the awkward cases. If you
Quick answer: normalise the text, strip accent marks, lowercase it, replace everything that is not a letter or digit with one hyphen, and trim the ends. The function below does that and handles the awkward cases. If you only need a slug once, paste your title into the Slug Generator.
The function
const SPECIAL = { "Γ": "ss", "Γ¦": "ae", "Ε": "oe", "ΓΈ": "o", "Δ": "d", "Ε": "l" };
function slugify(input, { separator = "-", maxLength = 60 } = {}) {
const sep = separator;
let s = String(input ?? "")
.normalize("NFKD") // split letters from accents: e-acute -> e + accent mark
.replace(/[\u0300-\u036f]/g, "") // drop the accent marks
.replace(/[ΓΓ¦ΕΓΈΔΕ]/gi, (c) => SPECIAL[c.toLowerCase()])
.replace(/&/g, " and ")
.toLowerCase()
.replace(/[^a-z0-9]+/g, sep) // anything else becomes one separator
.replace(new RegExp(`^${sep}+|${sep}+$`, "g"), ""); // trim separators
if (s.length > maxLength) {
s = s.slice(0, maxLength);
const cut = s.lastIndexOf(sep); // do not end in half a word
if (cut > maxLength * 0.6) s = s.slice(0, cut);
s = s.replace(new RegExp(`${sep}+$`, "g"), "");
}
return s;
}
What it returns
| Input | Output |
|---|---|
How to Convert HEIC to JPG for Free! |
how-to-convert-heic-to-jpg-for-free |
Rock & Roll: Cafe Guide |
rock-and-roll-cafe-guide |
10 Tips (2026) |
10-tips-2026 |
CrΓ¨me brΓ»lΓ©e β the easy way |
creme-brulee-the-easy-way |
--Hello___World-- |
hello-world |
ΓnΓ―cΓΆdΓ© StraΓe |
unicode-strasse |
These cases were run in Node before publishing.
Why each step is there
-
normalize("NFKD")splits an accented letter into the plain letter plus an accent mark, so the next line can remove the mark and keep the letter. -
The special letters map is needed because
Γ,ΓΈand a few others do not decompose. Without itStraΓebecomesstra-e. -
&becomes "and" soRock & Rollkeeps its meaning. -
One separator for any run of symbols avoids ugly
--and-_-sequences. - Trimming removes a leading or trailing hyphen.
- Length cut at a word boundary stops a slug ending in half a word.
Limits to know about
This function keeps only English letters and digits. Titles in Arabic, Urdu, Hindi, Chinese and other scripts would become an empty string. For those, either transliterate first or keep a short English slug. Always check for an empty result, and handle duplicates in your own code, for example by adding -2.
The slug rules, short version
Lowercase, hyphens between words, three to six words, no accents or symbols, and do not change a slug after publishing without a redirect. The full list with examples is in How to make an SEO-friendly URL slug.
Related
URL encoder and decoder for percent-encoding, and Text Case Converter.
Originally published on SaveAnyReel. I wrote this with AI assistance and ran the code in Node before publishing.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.