Shahid Alam
Shahid Alam

Reputation: 41

split function is not working with the regex provided

this is my take on the problem. i have successfully passed 3/4. the only thing messing up my solution is that extra white space after ‘telebubbles’. how do i progress further? i’m not good with replace.

function spinalCase(str) {
let k=str.split(/[\s-_]?(?=[A-Z])/g).join('-').toLowerCase()
console.log(k)
return k
}

spinalCase('This Is Spinal Tap');//passed
spinalCase("thisIsSpinalTap")//passed
spinalCase("The_Andy_Griffith_Show")//passed
spinalCase("Teletubbies say Eh-oh")//not working
spinalCase("AllThe-small Things")//passed

Upvotes: 2

Views: 47

Answers (1)

anubhava
anubhava

Reputation: 785256

You may use this regex to fix your problem:

/\s+|[-_]*(?=[A-Z])/

Code:

function spinalCase(str) {
    let k = str.split(/\s+|[-_]*(?=[A-Z])/g).join('-').toLowerCase();
    console.log(k);
    return k;
}

spinalCase('This Is Spinal Tap'); //passed
spinalCase("thisIsSpinalTap"); //passed
spinalCase("The_Andy_Griffith_Show"); //passed
spinalCase("Teletubbies say Eh-oh"); //passed
spinalCase("AllThe-small Things"); //passed

RegEx Details:

  • \s+: Match 1+ whitespaces
  • |: OR
  • [-_]*(?=[A-Z]): Match 0 or more of underscore or hyphen if that followed by an uppercase letter

Upvotes: 4

Related Questions