Slugify
Turn any string into a URL-friendly slug. Lowercases, removes accents, replaces spaces with hyphens.
About Slugify
Slugify converts arbitrary text — a blog post title, product name, or section heading — into a slug suitable for use in a URL. Accents are stripped via Unicode normalization, characters outside the ASCII letter/digit set are removed, runs of whitespace and punctuation collapse to a single hyphen, and the result is lowercased. The output is safe to use in any URL path component without escaping.
When to use it
- Generating SEO-friendly URLs from article titles
- Producing file names from user-supplied strings
- Building anchor links from heading text
- Creating stable IDs from human-readable labels in a CMS or design tool
How it works
The string is normalized to NFD, which decomposes accented characters into a base letter plus combining marks. The combining marks are then removed by a regex. Anything not [a-z0-9] is collapsed into a single hyphen, leading and trailing hyphens are trimmed, and the result is lowercased.
Examples
Hello, World!
hello-world
Café résumé — naïve approach
cafe-resume-naive-approach
Multiple spaces & symbols!
multiple-spaces-symbols
Frequently asked questions
- Does it handle non-Latin alphabets (Cyrillic, Chinese, Arabic)?
- No. Characters outside [a-z0-9] are stripped after normalization, so Cyrillic, Greek, Chinese, Arabic, and other non-Latin text becomes an empty slug or a slug consisting only of any embedded digits. For transliteration, pre-process with a dedicated library like transliteration or unidecode.
- Can I customize the separator?
- The default is a hyphen, which matches RFC 3986 URL conventions. To use underscores or another character, post-process with the find-and-replace tool.
- Is the output stable?
- Yes. Running the same input through slugify twice produces the same output (the function is idempotent). This is important for building stable IDs you can reference later.
- What about reserved characters?
- The output only contains lowercase letters, digits, and hyphens — all of which are unreserved in RFC 3986. No URL encoding is needed.