const toTitleCase = str => str.replace(/(^w|sw)(S*)/g, (_,m1,m2) => m1.toUpperCase()+m2.toLowerCase()) console.log(toTitleCase("heLLo worLd")); // Hello World