Soluția găsită de mine:
function myReplace(str, before, after) { // convert before & after to array if ( before.charAt(0) == before.charAt(0).toUpperCase() ) { after = after.charAt(0).toUpperCase() + after.slice(1); //after = after.split(''); //after[0] = after[0].toUpperCase(); //after = after.join(''); } return str.replace(before, after); } console.log(myReplace("His name is Tom", "Tom", "john"));