My quick solution:
function myReplace(str, before, after) {
return str.split(before).join(before[0] == before[0].toUpperCase() ? after.charAt(0).toUpperCase() + after.slice(1): after);
}
myReplace(“He is Sleeping on the couch”, “Sleeping”, “sitting”);