Slugify a string for URLs
Convert any title into a lowercase, hyphenated, accent-free slug that is safe to use in a URL.
- JavaScript
- GreenSnippets
- MIT
- Executed with Node.js 22
function slugify(text, maxLength = 80) {
return text
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, maxLength)
.replace(/-+$/g, "");
}Example / expected output
slugify("Crème Brûlée: the 5-minute recipe!") // "creme-brulee-the-5-minute-recipe"How it works
Unicode normalization (NFKD) splits accented letters into a base letter plus a combining mark, which the regex then removes. Everything that is not a letter or digit becomes a single hyphen, and leading or trailing hyphens are trimmed. Check uniqueness in your database separately.
Related snippets
- Python Tested
Slugify a string
Turn text into a lowercase, hyphenated, ASCII-only slug using only the standard library.
- PHP Tested
Slugify a string in PHP
Create URL-safe lowercase slugs from titles, removing accents and punctuation.
- HTML
Essential head tags for SEO and sharing
The meta, canonical, Open Graph and icon tags every public page should have.
- JavaScript Tested
Truncate a string with an ellipsis
Shorten text to a maximum length without cutting in the middle of a word or a Unicode character.

