Scriptdrill

Level 3: Strings and dates Exercise 19 of 45 Pro

Make a URL slug

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Turn a title into a URL slug: lowercase, accents removed (é becomes e), and every run of characters that are not a to z or 0 to 9 replaced by a single hyphen. No hyphen at the start or end. Characters with no plain-letter form, such as 日本, are dropped.

What this exercise teaches

NFD normalization separates letters from their accents so the accents can be deleted. Replacing whole runs of other characters with one hyphen avoids doubled hyphens, and the last replace trims the ends.

A hint if you get stuck

`normalize("NFD")` splits é into e plus a combining accent, and `/[\u0300-\u036f]/g` matches those accents. Then lowercase, replace `/[^a-z0-9]+/g` with `"-"` and trim hyphens from the ends.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Strings and dates