Regex for credit card number

/\b(?:\d[ -]?){12,18}\d\b/g

Options
Matches

Examples

Note

Finds 13–19 digit runs with optional spaces or dashes. Whether a number is real is a Luhn checksum, not a regex.

Matches in the example

  • "4111 1111 1111 1111"
  • "5500-0000-0000-0004"

Token by token

  1. \b a word boundary
  2. (?: a group, not capturing
  3. \d a digit (0–9)
  4. [ -]? one of: a space or “-”, optionally
  5. ){12,18} end of group, whole group between 12 and 18 times, as many as possible
  6. \d a digit (0–9)
  7. \b a word boundary

Other patterns