javascript string replace regex code example
Example 1: replace regex javascript
const p = 'lazy dog. If the dog reacted, was it really lazy?';
const regex = /Dog/ig;
p.replace(regex, 'ferret')
Example 2: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 3: string replace javascript
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."
Example 4: str replace javascript all
str.replace(/abc/g, '');
Example 5: javascript string replace
const p = 'Its going to rain today and its going to rain tomorrow';
const regex = /rain/gi;
console.log(p.replace(regex, 'snow'));
console.log(p.replace('rain', 'snow'));
Example 6: replace javascript
let randomtext = "Random text yo!"
let textRegex = /yo!/;
randomtext.replace(textRegex, "is here.");
console.log(randomtext.replace(textRegex, "is here."));