The Ultimate Way to Slugify a URL String in JavaScript

Turn “this URL-optimized string” into “this-url-optimized-string” easily with this snipped of JavaScript code.

Matthias Hagemann
Feb 6, 2018 · 1 min read

This tutorial was originally published on Gatemill:

With the advent of Node.js and modern application endpoint routing, I find it surprising that there is no in-built function to convert any string into an URL-optimized slug.

If your application users write content and you want to make their content searchable with an SEO-friendly URL, a slugify function is practically inevitable.

In order to turn an article’s original title “Why Uber isn’t spelled Über” into a slug optimized for URLs, parse your string through this slugify function to get “why-uber-isnt-spelled-uber”. I’ve pasted this Slugify function in a public Gist here.

Update 02/18/2019

This code snippet has been updated to include additional characters contributed by Christian Sarnataro and Paul Alexandru.

Update 06/29/2019

Added several more special characters.

Update 07/23/2019

Added special characters suggested by ahmet to support the Turkish language.

Matthias Hagemann

Written by

I’m the founder and CEO at Overscout where we build cloud API and SaaS brands, one at a time. I also like to create online businesses. https://hagemann.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade