JavaScript Slug Generator

Create clean URL slugs in pure JavaScript — no dependencies required, in the browser or Node.js. The core trick is String.prototype.normalize("NFKD"), which decomposes accented characters (é → e + combining accent) so a single regex can strip the diacritics; without it, "café" becomes "caf" or keeps the accent, breaking URLs on older systems. The snippet below also collapses repeated separators and trims leading/trailing hyphens — the two edge cases most hand-rolled versions miss. Where you will use this: generating slugs live as a user types a title (pair it with a debounced input handler), building static-site permalinks at build time, or cleaning CMS titles before sending them to an API. Watch out for languages NFKD cannot map (Cyrillic, Greek, CJK): normalize() leaves them untouched, so either allow Unicode slugs (modern browsers and Google handle them fine) or add a transliteration map like the one this tool applies when you pick a language above. For high-volume Node.js services, cache slug lookups — the regex work is cheap but uniqueness checks against a database are not. Test any title above and copy the function below.

function slugify(text) {
  return text
    .toString()
    .normalize('NFKD')
    .replace(/[\u0300-\u036f]/g, '') // remove accents
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9 -]/g, '')
    .replace(/\s+/g, '-')
    .replace(/-+/g, '-');
}

slugify('Hello World! SEO URL'); // "hello-world-seo-url"