I want to replace "
and /
from my string
but this string function only replaces the first occurrence of the character and leaves rest
exports.customerCancel =functions.database.ref(`/test`)
.onUpdate(async(change, context) => {
var status = change.after.val();
sta = String(status).replace("a","m");
database.ref(`/result`).set(sta);
});
how can I replace all characters?
CodePudding user response:
You can use global
flag with RegEx to remove all occurrences of those characters:
exports.customerCancel =functions.database.ref(`/test`)
.onUpdate(async (change, context) => {
const status = change.after.val();
const sta = status.replace(/["\\]/g,"")
// ^ ^<--global flag
// characters to remove -->^
return database.ref(`/result`).set(sta);
});
Read more about RegEx Global flag at MDN.