Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of '4111111111111111' |
(?: |
Start of non-capturing group for alternation | N/A |
4[0-9]{12} |
Visa: starts with 4, followed by 12 digits | 4111111111111 |
(?:[0-9]{3})? |
Optional group: 3 digits (Visa 16-digit variant) | 4111111111111111 |
| |
Alternation (OR operator) | N/A |
5[1-5][0-9]{14} |
Mastercard: starts with 51-55, followed by 14 digits | 5555555555554444 |
| |
Alternation (OR operator) | N/A |
3[47][0-9]{13} |
Amex: starts with 34 or 37, followed by 13 digits | 378282246310005 |
| |
Alternation (OR operator) | N/A |
3(?:0[0-5]|[68][0-9])[0-9]{11} |
Diners Club: starts with 300-305 or 36-38, followed by 11 digits | 30569309025904 |
| |
Alternation (OR operator) | N/A |
6(?:011|5[0-9]{2})[0-9]{12} |
Discover: starts with 6011 or 65, followed by 12 digits | 6011111111111117 |
| |
Alternation (OR operator) | N/A |
(?:2131|1800|35\d{3})\d{11} |
JCB: starts with 2131, 1800, or 35 followed by 3 digits, then 11 digits | 3530111333300000 |
) |
End of non-capturing group for alternation | N/A |
$ |
Anchor: asserts position at end of the string | End of '4111111111111111' |
Matches
4111111111111411111111111111155555555555544443782822463100053056930902590460111111111111173530111333300000
Does not match
123456789012345641111111111111151051051051051003411111111111116011111111111113528000700000000
Common pitfalls
- Forgetting to escape backslashes in JavaScript strings (e.g., using \d instead of \d).
- Assuming this pattern validates Luhn checksum (it only checks issuer patterns).
- Not anchoring the pattern (^ and $) leading to partial matches.
- Mistaking the Visa pattern for 13 or 16 digits without considering the optional group.
Performance notes
The alternation is well-structured with no catastrophic backtracking risk. Each branch is mutually exclusive and quantifiers are bounded. Anchoring ensures full-string matching.