Q:

js replace characters in a string

var str = "Visit Websites!";

var res = str.replace("Websites", "Grepper");
 
2
var str = "Original string!";
var str = str.replace("Original", "New");
3
str.replace(/abc/g, '');
8
str.replace(/hello/g, 'hi');
// the g is to show it's a global change, not one-time change.

str.replace(new RegExp('hello', 'g'), 'hi');
1
let a = "How to search and replace a char in a string";
while (a.search(" ", ""))
{
   a = a.replace(" ", "");
}
console.log(a);
0

New to Communities?

Join the community