DevLab
T
Textbeginner

Slug Generator

convert any text to a URL-friendly slug

By Bikram NathLast updated

Converting a post title like 'Hello World! (2024)' into the URL path 'hello-world-2024' takes one paste: spaces become hyphens, punctuation is stripped, and the whole string lowercases. Unlike a case converter, it also applies Unicode NFD normalization, turning 'café' into 'cafe' rather than a percent-encoded sequence. Custom separators let you output underscores instead of hyphens when your system requires it.

Try it now — free, instant, no signup

What is Slug Generator?

A slug generator takes arbitrary text and produces a string safe for use in a URL path segment. Feed it 'Best Node.js Frameworks 2024 Edition' and it returns 'best-node-js-frameworks-2024-edition': everything lowercased, spaces replaced with your chosen separator, and characters outside the URL-safe set stripped or normalized away.

Reach for this instead of a terminal one-liner when you need to preview slugs interactively or you are on a machine without Node or Python available. A quick sed and tr pipeline handles basic space-to-hyphen replacement, but it will not touch Unicode normalization, so 'München' stays as 'münchen' in raw output while this tool strips the umlaut to give 'munchen'. If you are inside a Node project already, the 'slugify' npm package is the right permanent choice; this tool is faster for one-off checks or showing a teammate exactly what a given title will resolve to.

One technical note worth knowing: NFD decomposition breaks accented Latin characters like 'é' into a base letter plus a combining diacritic, and then the non-ASCII diacritic is stripped. That works cleanly for most European scripts. It does not work for Chinese, Arabic, Korean, or any logographic or abjad script, because those code points have no ASCII base decomposition. The result for those inputs is an empty string or just separators. Transliteration (a separate preprocessing step) is required before slugifying non-Latin content.

When to use Slug Generator

Verify that a CMS slug for a post titled 'Q&A: 10 Tips!' matches your routing expectations before saving the record.
Generate consistent, lowercase identifiers from product names when writing a catalog import script against a URL-keyed API.
Test whether switching from hyphens to underscores would affect the length or readability of your URL scheme before committing to a convention.

Frequently Asked Questions

Why does 'café' become 'cafe' but Chinese characters produce an empty slug?
Unicode NFD normalization decomposes 'é' into the base character 'e' plus a combining accent (U+0301). Stripping non-ASCII code points then removes the accent, leaving 'e'. Chinese, Japanese, Arabic, and other non-Latin scripts do not decompose into ASCII base characters under NFD, so after normalization and stripping they contribute nothing to the output. If your content is multilingual, run a transliteration step first using a library like 'anyascii' or 'transliteration' in Node, then pass the result through slugify.
What is the difference between this and running kebab-case in a case converter?
Kebab-case conversion only handles capitalization and word boundaries: 'helloWorld' becomes 'hello-world'. It does not strip characters that break URL parsing, such as question marks, hash signs, square brackets, or percent signs. Running kebab-case on 'Buy Now! (Limit: 5)' gives 'buy-now!-(-limit:-5-)' which corrupts URL structure; a slug generator gives 'buy-now-limit-5'. The slug generator also collapses consecutive separators and trims leading or trailing hyphens, which kebab-case does not.
Should I use hyphens or underscores for SEO?
Google's crawler documentation recommends hyphens because Googlebot treats them as word separators and treats underscores as word joiners. 'slug_generator' reads to the indexer as the single token 'sluggenerator', while 'slug-generator' is indexed as two separate terms 'slug' and 'generator', which broadens keyword matching. WordPress, Ghost, and Contentful all default to hyphens for this reason. Use underscores only when a downstream system enforces them, such as matching a database column name or an identifier in a legacy API.
What happens to consecutive hyphens or separators at the edges?
Standard slug conventions collapse multiple consecutive separators into one and trim any separator at the start or end. The sequence goes: replace spaces and punctuation with hyphens, producing 'Hello---World!!!', then strip remaining non-alphanumeric characters ('Hello---World'), then collapse runs ('Hello-World'), then lowercase ('hello-world'). Leading or trailing separators appear when the original text starts or ends with punctuation, for example '(Draft) Post' would otherwise yield '-draft-post'. The trim step removes those to give 'draft-post'. Watch for this when inputs begin with parentheses, brackets, or numbers followed by a period.
How do I reproduce this tool's output in Node.js for a build script?
The core pipeline maps to chained string operations: NFD-normalize, strip combining diacritics, lowercase, remove non-alphanumeric non-separator characters, collapse separator runs, and trim. In code: str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().replace(/[^a-z0-9\s-]/g, '').trim().replace(/[\s-]+/g, '-'). For production use, the 'slugify' npm package by simov mirrors this behavior and adds locale-aware character tables for extended Latin and symbol sets. Most frameworks, including Next.js's built-in router, delegate to similar logic internally.

Related Tools