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.
Input: title = "Hello, World!" Output: "hello-world"
, between the words becomes one dash, and the ! at the end is dropped.
Input: title = " React 19: What's New? " Output: "react-19-what-s-new"
The apostrophe also counts as a separator.
0 <= title.length <= 300title may contain any characters.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).
I'm here to help with #605 Slug for a Blog Post URL. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.