string replace regex javascript code example
Example 1: javascript replace string
var str = "JavaScript replace method test";
var res = str.replace("test", "success");
Example 2: js replace characters in a string
var str = "Original string!";
var str = str.replace("Original", "New");
Example 3: replace regex javascript
const p = 'lazy dog. If the dog reacted, was it really lazy?';
const regex = /Dog/ig;
p.replace(regex, 'ferret')
Example 4: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 5: 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 6: str replace javascript all
str.replace(/abc/g, '');