Q:

Replace function in string in javascript

var str = "JavaScript replace method test";
var res = str.replace("test", "success");  
//res = Javscript replace method success
17

var str = "Mr Blue has a blue house and a blue car";

var res = str.replace(/blue/g, "red");
 
12
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
const p = 'hello world ! hello everyone ! ';

const regex = /hello/gi;

console.log(p.replace(regex, 'good morning'));
// expected output: "good morning world ! good morning everyone !"

console.log(p.replace('hello', 'good evening'));
// expected output: "good evening world ! hello everyone !"
1
var str = "Hello World";
var res = str. reaplace("World", "Simon");
output result = Hello simon
0

New to Communities?

Join the community