For some reason, this answer I found for (supposedly) how to do it in php just gives me the wrong matches. It seems to add the dash, but also replace the capital letter wit
var res = yourString.replace(/[A-Z]/g, "-$&").toLowerCase();
Try the following:
var result = "fooBarBaz".replace(/([A-Z])/g, "-$1").toLowerCase(); console.log(result);