Share your repls and programming experiences

← Back to all posts
Search and Replace Intermediate Algorithms free code camp
oscarhernandez6 (0)

/*function myReplace(str, before, after) {
var result=[];
var arr = after.replace(/john/i, "John" ).replace(/algorithms/i, "Algorithms" ).replace(/spelling/i, "spelling" ).replace(/mall/i, "mall" ).replace(/sitting/i, "Sitting" );
myFunction();
function myFunction(){
result = str.replace(before, arr);

}
console.log(result)
return result;
}

myReplace("Let us get back to more Coding", "Coding", "algorithms");
//mall|algorithms|john|sitting|spelling.
*/

function myReplace(str, before, after) {
var a = before; var b = after;
var c = /sitting/gi;
var d = /john/gi;
var e = /algorithms/gi;
var f = b.replace(c, "Sitting").replace(d, "John").replace(e, "Algorithms");
var g = str.replace(a, f); console.log(g)
return g;
}

myReplace("Let us go to the store", "store", "mall");
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
myReplace("This has a spellngi error", "spellngi", "spelling");
myReplace("His name is Tom", "Tom", "john");
myReplace("Let us get back to more Coding", "Coding", "algorithms");

/Note: skip the comments to test the firt function"./

Commentshotnewtop
AdCharity (1101)

? Isn't that just regex?

AdCharity (1101)

@oscarhernandez6 my point is that search/replace functions are built into javascript. .replace(/pattern (called Regex)/,"replaceWith")

oscarhernandez6 (0)

ok, but did not work with out the //gi; @AdCharity