← All posts
3 min readBy

Write a Slugify Function in JavaScript (Accents, Symbols and Edge Cases)

A small, tested JavaScript slugify function that handles accents, ampersands, extra symbols and long titles, plus the mistakes that break URL slugs.

javascriptseoguides
Write a Slugify Function in JavaScript (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 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 + ́
    .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 é into e 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 it Straße becomes stra-e.
  • & becomes "and" so Rock & Roll keeps 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.