Answers for "loop replace javascript"

31

javascript replace

var res = str.replace("find", "replace");
Posted by: Guest on October-21-2019
10

javascript replace

const p = 'dog dog cat rat';
const regex = /dog/gi;

console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"

console.log(p.replace('dog', 'cow'));
// If pattern is a string, only the first occurrence will be replaced.
//output: "cow dog cat rat"
Posted by: Guest on July-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language