开发者

Javascript regex .match() is null

开发者 https://www.devze.com 2023-02-11 09:55 出处:网络
console.log(r.message); //returns \"This transaction has been authorized\" if (r.message.match(/approved/).length > 0 || r.message.match(/authorized/).length > 0) {
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

Is this not the correct way to do matching in JavaScri开发者_如何学Pythonpt?

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


Since you are getting authorized message the statement r.message.match(/approved/) will return null and hence the issue.

Rewrite the check as follows:

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


Just do:

if (r.message.match(/approved/) || r.message.match(/authorized/)) {
  ...
}


Use .search() instead of .match() if you're using it in comparison to numbers.

--> example <--

0

精彩评论

暂无评论...
验证码 换一张
取 消