我需要刷卡,我正在创建一个现场 Kioske,需要在刷卡后预填一张信用卡表格。我不确定为什么它无法识别任何卡片。这是一个滑动示例:
%B5500005555555559"TORRANCEJACK G P "2009206000000000000326000000
有人可以解释为什么它没有通过以下正则表达式模式 exec 吗?
// MasterCard starts with 51-55, and is 16 digits long.
var pattern = new RegExp("^%B(5[1-5][0-9]{14})\\^([A-Z ]+)/([A-Z ]+)\\^([0-9]{2})([0-9]{2})");
var match = pattern.exec(rawData);
谢谢!
最佳答案
我建议您使用以下模式:
/%B(5[1-5][0-9]{14})"([A-Z ]+?)\s+"([0-9]{2})([0-9]{2})/
参见 regex demo .请注意,您必须微调最后的 ([0-9]{2})([0-9]{2})
组,以便将正确的数字放入必要数量的组中。
JS 演示:
var rx = /%B(5[1-5][0-9]{14})"([A-Z ]+?)\s+"([0-9]{2})([0-9]{2})/g;
var s = '%B5500005555555559"TORRANCEJACK G P "2009201800000000000326000000';
var matches = rx.exec(s);
if (matches) {
console.log("Number: " + matches[1]); // => number
console.log("Name: " + matches[2]); // => name
console.log("Exp. year: " + matches[3]); // => exp year
console.log("Exp. month: " + matches[4]); // => exp month
}
关于javascript - 为什么这个正则表达式模式没有传入 exec 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46658510/