Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|35(?:2[89]|[3-8][0-9])[0-9]{13})$

This regex matches valid credit card numbers according to the major card issuer patterns (Visa, MasterCard, Amex, Discover, Diners Club, JCB, etc.).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '4111111111111111'
(?: Non-capturing group start: groups alternatives without capturing Start of group for Visa cards
4[0-9]{12} Visa cards: starts with 4, followed by 12 digits 4111111111111
(?:[0-9]{3})? Optional group: 3 digits (Visa 16-digit cards) 123
| Alternation: separates alternatives in the group Separates Visa from MasterCard patterns
5[1-5][0-9]{14} MasterCard: starts with 51-55, followed by 14 digits 5555555555554444
| Alternation: separates alternatives in the group Separates MasterCard from Amex patterns
3[47][0-9]{13} Amex: starts with 34 or 37, followed by 13 digits 378282246310005
| Alternation: separates alternatives in the group Separates Amex from Diners Club patterns
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: separates alternatives in the group Separates Diners Club from Discover patterns
6(?:011|5[0-9]{2})[0-9]{12} Discover: starts with 6011 or 65, followed by 12 digits 6011111111111117
| Alternation: separates alternatives in the group Separates Discover from JCB patterns
35(?:2[89]|[3-8][0-9])[0-9]{13} JCB: starts with 3528-3529 or 353-358, followed by 13 digits 3530111333300000
) Non-capturing group end End of group for all card patterns
$ Anchor: asserts position at end of the string End of '4111111111111111'

Matches

  • 4111111111111
  • 4111111111111111
  • 5555555555554444
  • 378282246310005
  • 30569309025904
  • 6011111111111117
  • 3530111333300000

Does not match

  • 1234567890123456
  • 411111111111111
  • 5105105105105100
  • 340000000000000
  • 6500000000000000
  • 3527000000000000
  • abc123def456

Common pitfalls

  • Forgetting to anchor with ^ and $ may match partial strings or invalid prefixes/suffixes
  • Not accounting for optional groups (e.g., Visa 13 vs 16 digits) can cause mismatches
  • Assuming all card numbers are 16 digits; patterns include 13, 14, 15, and 16-digit cards
  • Overlooking that Diners Club and JCB have specific ranges beyond the initial digits

Performance notes

The regex is well-structured with non-capturing groups and alternations ordered by likelihood, minimizing backtracking. However, very long strings (e.g., 1000+ chars) may cause performance issues due to the {n} quantifiers. Anchoring with ^ and $ is critical to prevent partial matches.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why does the regex include both 13 and 16-digit Visa patterns?

Visa cards can be 13 or 16 digits long, so the pattern accounts for both: 4 followed by 12 digits (13 total) or 4 followed by 12 digits and an optional 3 digits (16 total).

Does this regex validate Luhn checksums?

No, this regex only validates the card number format and issuer patterns. Luhn checksum validation requires additional logic beyond regex.

Why are some patterns like 3[47][0-9]{13} and not 3[47][0-9]{14}?

Amex cards are 15 digits long (34 or 37 followed by 13 digits), so the pattern reflects the correct length. The {13} quantifier ensures 15 total digits.