Answers for "how to replace a string with another string in 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
0

replace each string by another string javascript

str.split(search).join(replacement)
Posted by: Guest on February-05-2021

Code answers related to "how to replace a string with another string in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language