regex for credit card number

match if the string is a credit card

(^4[0-9]{12}(?:[0-9]{3})?$)|(^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$)|(3[47][0-9]{13})|(^3(?:0[0-5]|[68][0-9])[0-9]{11}$)|(^6(?:011|5[0-9]{2})[0-9]{12}$)|(^(?:2131|1800|35\d{3})\d{11}$)
4569403961014710 5191914942157165 370341378581367 38520000023237 6011000000000000 3566002020360505 1234566660000222

Credit card number is the card unique identifier found on payment cards.

This expression can be used to detect or verify credit card numbers:

  • Visa (group #1)
  • MasterCard (group #2)
  • American Express (group #3)
  • Diners Club (group #4)
  • Discover (group #5)
  • JCB (group #6)

Disclaimer:
If you want to use regex just to know the card brand for visual use (display Visa logo or label), that is fine.
But if your code logic depends on it, then don't use regex, and use a 3rd party plugin/library instead (read more about it here).


Cheatsheet

expr usage
/[0-9]/ matches all digits
/(hello){1,3}/ matches "hello" that occur between 1 and 3 times (inclusive)
iHateRegex
by geon