Javascript 正则表达式 .match() 为空
2011-02-15
12363
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);
}
},
3个回答
由于您收到了授权消息,语句
r.message.match(/approved/)
将返回 null,从而导致问题。
按如下方式重写检查:
if (/approved|authorized/.test(r.message)) {
Chandu
2011-02-15
只需执行:
if (r.message.match(/approved/) || r.message.match(/authorized/)) {
...
}
CanSpice
2011-02-15
使用.search()而不是.match(),如果您使用的是数字。
- > 示例 < -
Shaz
2011-02-15