Created
March 27, 2016 10:10
-
-
Save samkahchiin/262ce152520f3f1e4e3b to your computer and use it in GitHub Desktop.
freecodecamp : Search and Replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myReplace(str, before, after) { | |
var regex = new RegExp(before); | |
if(before[0] === before[0].toUpperCase()){ | |
after = after[0].toUpperCase() + after.slice(1) ; | |
} | |
return str.replace(regex,after); | |
} | |
myReplace("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