Skip to content

Instantly share code, notes, and snippets.

@IllusionElements
Created August 4, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IllusionElements/bdb56a0775ef6b458e9a to your computer and use it in GitHub Desktop.
Save IllusionElements/bdb56a0775ef6b458e9a to your computer and use it in GitHub Desktop.
function replace(str, before, after) {
var aslice = after.slice(1);
if(before[0] === before[0].toUpperCase()){
var caps = after.charAt(0).toUpperCase() + aslice;
return str.replace(before, caps);
}
else if(after[0] === after[0].toUpperCase() && before[0] === before[0].toLowerCase()){
var low = after.charAt(0).toLowerCase() + aslice;
return str.replace(before, low);
}
else
return str.replace(before,after);
}
replace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
replace("A quick brown fox jumped over the lazy dog", "jumped", "Leaped");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment