I want to convert this text
font-size-18
to this
fontSize18
using Regex. Here is my trail:
let conv = str => str.replace(/\-[a-z]/gi,/[A-Z]/gi);
but it doesn't work, I can do it without Regex. But I want to make it with Regex, can this be done using regex?
Match the dash followed by a single character, and use a replacer function that returns that character toUpperCase
:
const dashToCamel = str => str.replace(/-(\w)/g, (_, g1) => g1.toUpperCase());
console.log(dashToCamel("font-size-18"));
This assumes that all dashes are followed by word characters (though you could use .
instead of \w
). Note that since it looks like you want to replace all dashes, you should match and replace all dashes, not just dashes followed by alphabetical characters.