Regex to Match a Credit Card Number
Copy the pattern, paste text below, and watch it highlight card numbers by brand. Below the tester: what each branch matches, and why regex is only a format check.
What this pattern matches
This matches the digit format of the four major card brands. It is a format check only — it does not verify the Luhn checksum or that a card is real.
How it works, part by part
4[0-9]{12}(?:[0-9]{3})? — Visa: starts with 4, 13 or 16 digits.
5[1-5][0-9]{14} — Mastercard: starts 51–55, 16 digits.
3[47][0-9]{13} — American Express: starts 34 or 37, 15 digits.
6(?:011|5[0-9]{2})[0-9]{12} — Discover.
Important
Use this to detect or redact card-shaped numbers, never to validate payments. Real validation needs the Luhn algorithm and a payment processor. Do not log or store raw card numbers.
FAQ
Does it check the Luhn checksum?
No. Regex only checks the digit format. Add a Luhn check in code to catch typos.
Can I use it to redact cards from logs?
Yes — that is a good use. Match and replace with a masked value.