Home > OS >  how to replace all selected character from string in firebase functions
how to replace all selected character from string in firebase functions

Time:09-26

I want to replace " and / from my string

enter image description here

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);
});

enter image description here

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.

  • Related