Skip to the code
cf.
+10 XP

#605 Slug for a Blog Post URL

easy

Your blogging tool builds each post's URL from its title, like /posts/hello-world.

Write toSlug(title) that lowercases the title and keeps only the letters a to z and the digits 0 to 9. Every run of other characters between two kept characters becomes a single -. The slug never starts or ends with -.

If no characters are kept, return an empty string.

Example 1

Input: title = "Hello, World!"
Output: "hello-world"

, between the words becomes one dash, and the ! at the end is dropped.

Example 2

Input: title = "  React 19: What's New?  "
Output: "react-19-what-s-new"

The apostrophe also counts as a separator.

Constraints

  • 0 <= title.length <= 300
  • title may contain any characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).