str replace in js code example
Example 1: javascript replace string
var str = "JavaScript replace method test";
var res = str.replace("test", "success");
Example 2: how to use the replace method in javascript
let string = 'soandso, my name is soandso';
let replaced = string.replace(/soandso/gi, 'Dylan');
console.log(replaced);
Example 3: javascript replace
exemplo = "Aprendendo Javascript. JAVAScript é na DevMedia ";
resultado = exemplo.replace(/javascript/gi, "JavaScript");
Example 4: javascript replace
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
console.log(p.replace('dog', 'cow'));
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'));