replaceText = function(string, oldText, newText) { index = string.indexOf(oldText); front = string.substring(0, index); back = string.substring(index + oldText.length); return front + newText + back; }; replaceTextGlobal = function(string, oldText, newText) { while(string.indexOf(oldText) >= 0) { string = replaceText(string, oldText, newText); } return string; }; console.log(replaceText("Randall","a","e")); console.log(replaceTextGlobal("Randall","a","e")); console.log(replaceText("Randall Reed","Randall","Randy"));