Jack Torrance
Jack Torrance

Reputation: 23

Why is this regex pattern not passing in the exec method?

I have a card swipe and I am creating a on site Kioske and need to prefill a credit card form after card swipe. I am not sure why It is not recognizing any cards. Here is an example of a swipe:

%B5500005555555559"TORRANCEJACK G P        "2009206000000000000326000000

Can someone explain why it is not passing the following regex pattern 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);

Thank you!

Upvotes: 1

Views: 95

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627065

I suggest that you use the following pattern:

/%B(5[1-5][0-9]{14})"([A-Z ]+?)\s+"([0-9]{2})([0-9]{2})/

See the regex demo. Note you will have to fine tune the last ([0-9]{2})([0-9]{2}) groups to get the right digits into the necessary number of groups.

JS demo:

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
}

Upvotes: 1

Munkhdelger Tumenbayar
Munkhdelger Tumenbayar

Reputation: 1874

Found it from here Here

^(?:5[1-5][0-9]\d{1}|222[1-9]|2[3-6][0-9]\d{1}|27[01][0-9]|2‌​720)([\ \-]?)\d{4}\1\d{4}\1\d{4}$

not sure about your regex working but says error to me on that slash /

Test this regex in Regex Tester this is working

Also i found useful something from here

If you need real program to prove working i make it for you if you need in snippet

Upvotes: 0

Related Questions