Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex .match() is null

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 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);
    }
},
like image 774
Webnet Avatar asked Feb 15 '11 18:02

Webnet


2 Answers

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)) {
like image 128
Chandu Avatar answered Nov 13 '22 12:11

Chandu


Just do:

if (r.message.match(/approved/) || r.message.match(/authorized/)) {
  ...
}
like image 2
CanSpice Avatar answered Nov 13 '22 12:11

CanSpice