I want the split method to include the arguement, how to?


function spinalCase(str) {
let reg = /[A-Z]/ 
str = str.split(reg)
  return str;
}


console.log(spinalCase("This Is Spinal Tap"))
console.log(spinalCase("thisIsSpinalTap"))
console.log(spinalCase("The_Andy_Griffith_Show"))
console.log(spinalCase("Teletubbies say Eh-oh"))
console.log(spinalCase("AllThe-small Things"))

I want the string to split on capital letters AND include those capital letters in the array assignments. How do you accomplish this?

I don’t know if you can use a variable for the argument in the split method. You have the right regex but have you tried without the variable?

Example:

function spinalCase(str) {
str = str.split(/[A-Z]/); //Regex as argument directly
  return str;
}

Or:

function spinalCase(str) {
str = str.split([A-Z]); // Without slashes
  return str;
}

Just some suggestions but I’m not positive if this will work for you or not.

MDN Web Docs: String.prototype.split()

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