Unsure how to handle last case

This works for 3/5 cases. How to separate string with no spaces.

function spinalCase(str) {
  console.log(str.replace(/[\s|_|A-Z]/g, "-"));
  return str.toLowerCase().replace(/[\s|_|A-Z]/g, "-");


Forget code for a minute. in your head, how do you split that string?

With the split method.

My brain doesn’t have a built-in split method as such. Nice try.

I mean, when you look at that string, you can mentally distinguish words. How? In your mind, where do you break it?

By seeing that new word starts with uppercase letter.

Sounds like a job for regular expressions!

But that’s what I tried. Something like console.log(str.split(/[\s|_|A-Z]/g)) doesn’t work.

Try looking into String.match(), as it’ll return an array of strings, one for each match to a particular regexp pattern.

Split removes the matching things, which in this case you don’t want.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.