gpt4 book ai didi

Javascript 正则表达式 .match() 为空

转载 作者:数据小太阳 更新时间:2023-10-29 04:02:47 25 4
gpt4 key购买 nike

console.log(r.message); //returns "This transaction has been authorized"
if (r.message.match(/approved/).length > 0 || r.message.match(/authorized/).length > 0) {
// ^ throws the error: r.message.match(/approved/) is null

这不是在 JavaScript 中进行匹配的正确方法吗?

success: function (r) {
$('.processing').addClass('hide');
if (r.type == 'success') {
console.log(r.message);
if (r.message.match(/approved/).length > 0 || r.message.match(/authorized/).length > 0) {
triggerNotification('check', 'Payment has been accepted');

//document.location = '/store/order/view?hash='+r.hash;
} else {
triggerNotification('check', r.message);
}
} else {
$('.button').show();

var msg = 'Unable to run credit card: '+r.message;

if (parseInt(r.code) > 0) {
msg = msg+' (Error code: #'+r.code+')';
}
triggerNotification('x', msg);
}
},

最佳答案

由于您正在获得授权消息,语句 r.message.match(/approved/) 将返回 null,因此会出现问题。

重写检查如下:

if (/approved|authorized/.test(r.message)) {

关于Javascript 正则表达式 .match() 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5007865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com