Peachy
0
Q:

javascript regex 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"
25
var str = "Visit Websites!";

var res = str.replace("Websites", "Grepper");
 
2

var str = "Visit Microsoft!";

var res = str.replace("Microsoft", "W3Schools");
 
8
str.split(search).join(replacement);
7
let re = /apples/gi; 
let str = "Apples are round, and apples are juicy.";
let newstr = str.replace(re, "oranges"); 
console.log(newstr)

output:

"oranges are round, and oranges are juicy."
0
str.replace(/abc/g, '');
8
str = str.replace(/abc/g, '');
3
const search = 'duck'
const replaceWith = 'goose';

const result = 'duck duck go'.replaceAll(search, replaceWith);

result; // => 'goose goose go'
2
str = str.replace(new RegExp('abc', 'g'), 'xyz');
2

New to Communities?

Join the community