Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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"
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #replace
ADD COMMENT
Topic
Name
1+9 =