I am trying to use .replace()
on a string to omit an underscore and camel case whatever string is passed through the function. I was able to get a " " and "-" omitted but am struggling using \W/ to omit a "_".
The small piece of code I have written is:
function toCamelCase(str) {
return str.replace(/\W+(.)/g, function(match, chr) {
return chr.toUpperCase();
});
}
console.log(toCamelCase("javaScript Exercises"));
console.log(toCamelCase("java-script-exercises"));
console.log(toCamelCase("java_script_exercises"))
I have it even so if the first letter is entered capital it remains capital and only runs camel case after.
If you are able to run the code snippet you will see the output:
javaScriptExercises
javaScriptExercises
java_script_exercises //my issue is here
I have seen a couple errors saying chr.upperCase()
is not a function in trying different versions of /\W+(.)/g
throughout the project. Is this not doable with .replace()
?