Skip to content

Instantly share code, notes, and snippets.

@keithn
Created July 27, 2020 07:39
Show Gist options
  • Save keithn/9c60f875a8c5721830b449526e47c818 to your computer and use it in GitHub Desktop.
Save keithn/9c60f875a8c5721830b449526e47c818 to your computer and use it in GitHub Desktop.
function myReplace(str, before, after) {
let isCapitalized = w => w[0].toUpperCase() === w[0]
let capitalize = w => w[0].toUpperCase() + w.slice(1)
let lower = w => w[0].toLowerCase() + w.slice(1)
let r = []
for(let w of str.split(" ")){
if(w.toLowerCase() === before.toLowerCase()) {
r.push(isCapitalized(w)?capitalize(after):lower(after))
} else r.push(w)
}
return r.join(" ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment