xxxxxxxxxx
const string = "a, b, c, d, e, f";
string.replace(/,/g, '');
console.log(string) //a b c d e f
xxxxxxxxxx
"x".replaceAll("x", "xyz");
// xyz
"x".replaceAll("", "xyz");
// xyzxxyz
"aA".replaceAll("a", "b", true);
// bb
"Hello???".replaceAll("?", "!");
// Hello!!!
xxxxxxxxxx
const replaceAll = (str, find, replace) => {
return str.replace(new RegExp(find, 'g'), replace);
};
// Example usage
const originalString = 'hello world';
const replacedString = replaceAll(originalString, 'o', 'a');
console.log(replacedString); // Output: hella warld
xxxxxxxxxx
let a = "How to search and replace a char in a string";
while (a.search(" ", ""))
{
a = a.replace(" ", "");
}
console.log(a);
xxxxxxxxxx
const str = 'Hello, world!';
const searchChar = 'o';
const replaceChar = 'x';
const replacedStr = str.split(searchChar).join(replaceChar);
console.log(replacedStr);