cara mengganti semua karakter dalam string javascript
const string = "a, b, c, d, e, f";
string.replace(/,/g, '');
console.log(string) //a b c d e f
KadzielawaKonrad
const string = "a, b, c, d, e, f";
string.replace(/,/g, '');
console.log(string) //a b c d e f
"x".replaceAll("x", "xyz");
// xyz
"x".replaceAll("", "xyz");
// xyzxxyz
"aA".replaceAll("a", "b", true);
// bb
"Hello???".replaceAll("?", "!");
// Hello!!!
function replaceAll(str, find, replace) {
var escapedFind=find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
return str.replace(new RegExp(escapedFind, 'g'), replace);
}
//usage example
var sentence="How many shots did Bill take last night? That Bill is so crazy!";
var blameSusan=replaceAll(sentence,"Bill","Susan");
var res = str.replace("find", "replace");
let a = "How to search and replace a char in a string";
while (a.search(" ", ""))
{
a = a.replace(" ", "");
}
console.log(a);