如何在 firebase 函数中替换字符串中的所有选定字符
how to replace all selected character from string in firebase functions
我想从我的字符串
中替换 "
和 /
但是这个字符串函数只替换第一次出现的字符并留下其余部分
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);
});
如何替换所有字符?
您可以使用带有 RegEx 的 global
标志来删除所有出现的这些字符:
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);
});
在 MDN 阅读有关 RegEx 全局标志的更多信息。
我想从我的字符串
中替换"
和 /
但是这个字符串函数只替换第一次出现的字符并留下其余部分
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);
});
如何替换所有字符?
您可以使用带有 RegEx 的 global
标志来删除所有出现的这些字符:
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);
});
在 MDN 阅读有关 RegEx 全局标志的更多信息。