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

Web*_*net 8 javascript regex

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
Run Code Online (Sandbox Code Playgroud)

这不是在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);
    }
},
Run Code Online (Sandbox Code Playgroud)

Cha*_*ndu 17

由于您获得了授权消息r.message.match(/approved/),因此该语句将返回null并因此返回问题.

重写支票如下:

if (/approved|authorized/.test(r.message)) {
Run Code Online (Sandbox Code Playgroud)