【发布时间】:2021-05-01 01:57:51
【问题描述】:
这里我使用解构从消息中提取数据。然后我需要在其范围之外使用它。我复制了相同的变量/常量以使其完全超出范围。如何避免这些重复的变量,直接在代码中使用重组后的变量/常量?
let OTP2, amount2, account2, payee2;
let channel;
switch (sender) {
case 'SBIINB':
const regexSBINetBankingFundTransfer = /.*?OTP.*?Rs. (?<amount>\d+) .*?(?<account>\d+)\s+to\s+(?<payee>.*?)\s+is\s(?<OTP>\d+)/m;
if(regexSBINetBankingFundTransfer.test(message)) {
let { groups: { account, amount, payee, OTP } } = regexSBINetBankingFundTransfer.exec(message);
account2 = account; amount2 = amount; payee2 = payee;
notificationType = 'fundTransfer';
} else {
const regexSBINetBankingLogin = /.*OTP.*(?<OTP>\d{8}).*/m;
if(regexSBINetBankingLogin.test(message)) {
let { groups: { OTP } } = regexSBINetBankingLogin.exec(message);
if (OTP != null) {
notificationType = 'login';
OTP2 = OTP;
} else {
notificationType = 'uncategorized';
}
}
}
编辑:没有声明策略的赋值不起作用
【问题讨论】:
-
对不起,没用。正如您从屏幕截图中看到的那样,OTP 变量在其范围之外不可用。
-
再次抱歉,它成功了!!!在上面的屏幕截图中,第 45 行隐藏了 OTP 变量。一旦我删除它,它就被识别出来了。非常感谢!
-
很高兴它成功了,您可以将此问题标记为重复问题 - 问题上应该有一个选项可以说明链接的答案回答了您的问题
标签: javascript ecmascript-6 regex-group