replace in string code example
Example 1: javascript remove text from string
var str = "That is like so not cool, I was like totally mad.";
var cleanStr = str.replace(/like/g, "");
Example 2: replace all occurrences of a string in javascript
const p = 'dog dog cat rat';
const regex = /dog/gi;
console.log(p.replace(regex, 'cow'));
Example 3: 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 4: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 5: str replace javascript all
str.replace(/abc/g, '');
Example 6: replace all javascript
str.split(search).join(replacement);