elefcode

Regex pattern

Credit Card Regex — free online tester

Match credit-card numbers from major networks (Visa, Mastercard, Amex, Discover) for extraction or redaction.

The pattern

/\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6011)[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/g

How it works

This regex matches the shape of credit-card numbers from the four major networks, including the common space- and dash-separated visual formats. It tells you "this string looks like a card number" — not "this card number is valid". Pair it with a Luhn check (the last-digit checksum) before treating any match as a real card.

When to use it

  • Redact card numbers from log files and crash reports
  • Prevent users accidentally pasting card numbers into chat / support tickets
  • Pre-flight check before sending to a payment processor
  • Find leaked card numbers in scraped data

Tip

NEVER store or transmit a matched card number — your stack is now in scope for PCI DSS. Use this only to detect and discard.

Try it on this input

Visa:        4532 1234 5678 9010
Mastercard:  5500-0000-0000-0004
Amex:        3400 000000 00009
Discover:    6011 1111 1111 1117
Compact:     4532123456789010
Not cards:   1234, 1234567890123, just text

What this pattern doesn't catch

  • Doesn't validate the Luhn checksum — use isLuhn() after extraction for true validation
  • Doesn't catch JCB, Diners, UnionPay, or other regional networks
  • Amex cards are 15 digits — the spacing format above matches; tighten if you only want 16-digit cards

More regex patterns