I have a task in Javascript that I need help with. The goal is to insert a special character between a lowercase and uppercase letter when they are matched together.
For example:
- myHouse => my_House
- aRandomString => a_Random_String
And so on...
To achieve this, I came up with the following regex pattern:
/([a-z][A-Z](.{0}))+/g
The issue I'm facing is if I use the replace method like this:
"aRandomString".replace(/([a-z][A-Z](.{0}))+/g, '_')
It will result in: _ando_tring
However, I found a solution which involves multiple steps. Is there a simpler way to accomplish this?
var mString = "aRandomString";
var match = mString.match(/([a-z][A-Z](.{0}))+/g, '_')
var save = [...match]
match = match.map(e => [e.slice(0, 1), '_', e.slice(1)].join(''))
save.forEach((s,i) => mString = mString.replace(s, match[i]))
console.log(mString)