how to replace characters in a string javascript code example
Example 1: replacing characters in string javascript
var myStr = 'this,is,a,test';
var newStr = myStr.replace(/,/g, '-');
console.log( newStr );
Example 2: js replace characters in a string
var str = "Original string!";
var str = str.replace("Original", "New");
Example 3: javascript regex replace
const search = 'duck'
const replaceWith = 'goose';
const result = 'duck duck go'.replaceAll(search, replaceWith);
result;
Example 4: replace all character in string javascript
let a = "How to search and replace a char in a string";
while (a.search(" ", ""))
{
a = a.replace(" ", "");
}
console.log(a);
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'));