I've been grappling with creating a function that transforms a string into "camelCase" format. At the moment, the function capitalizes letters following a hyphen -
and eliminates the hyphens entirely.
Here is the existing function:
function camelCase(str) {
return str.replace(/-([a-z])/g, g => {
return g[1].toUpperCase()
})
}
The string provided to the str
parameter may contain single forward slashes /
and hyphens -
alongside alphabetical characters.
I've experimented with various regex combinations in an attempt to solve this issue, but I seem to be stuck. How can I modify the regular expression /-([a-z])/g
to also exclude forward slashes?
// CURRENT OUTPUT
console.log(camelCase("folder/lower-case-with-dash"))
// folder/lowerCaseWithDash
// DESIRED OUTPUT
console.log(camelCase("folder/lower-case-with-dash"))
// folderLowerCaseWithDash
Your assistance on this matter would be greatly appreciated.