Create a simple URL slug
Normalize a Latin-text label into lowercase ASCII words separated by hyphens.
function slugify(value) { return value.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, ''); }
Input & expected output
Input
"Your order 8812 is out for delivery!"Expected output
"your-order-8812-is-out-for-delivery"The edge case
Input
"Café déjà vu"Expected output
"cafe-deja-vu"Know the limits
- This deliberately produces ASCII slugs. Many non-Latin scripts become empty.
- It does not guarantee uniqueness; preserve stable slugs separately from titles.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Convert a string to URL slug from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Curated implementation; explicit input bounds, edge cases, local fixtures and rebuilt presentation. Original MDX retained as inert source evidence.
Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/convert-a-string-to-url-slug.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.
Keep exploring
- Capitalize a string — Uppercase the first Unicode code point and preserve the remaining text.
- Reverse a string — Reverse Unicode code points without splitting surrogate pairs.
- Check a palindrome — Compare a string with its code-point reversal.