Skip to content

Instantly share code, notes, and snippets.

@alan2207
Created July 20, 2015 21:58
Show Gist options
  • Save alan2207/8faddf90f1735460da2a to your computer and use it in GitHub Desktop.
Save alan2207/8faddf90f1735460da2a to your computer and use it in GitHub Desktop.
function replace(str, before, after) {
var arr = str.split(" ")
var newArr = []
for(i=0;i<arr.length;i++){
if (arr[i]==before){
if(before.charAt(0).toUpperCase()){
after.split("")[0]=after.charAt(0).toUpperCase()
newArr.push(after)
}
}else{
newArr.push(arr[i])
}
var newStr = newArr.join(" ")
}
return newStr
}
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