Regex to Match Credit Card Numbers
Validate credit card number formats with this regex pattern. Matches Visa, Mastercard, American Express, and Discover card patterns. Free tool.
Regular Expression
/^(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{1,4}$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| (?: | Start of non-capturing group |
| 4 | Matches the literal character '4' |
| \d | Matches any digit (0-9) |
| {3} | Matches exactly 3 times |
| | | Alternation — matches the expression before OR after the pipe |
| 5 | Matches the literal character '5' |
| [1-5] | Character class — matches any one of: 1-5 |
| \d | Matches any digit (0-9) |
| {2} | Matches exactly 2 times |
| | | Alternation — matches the expression before OR after the pipe |
| 3 | Matches the literal character '3' |
| [47] | Character class — matches any one of: 47 |
| \d | Matches any digit (0-9) |
| {2} | Matches exactly 2 times |
| | | Alternation — matches the expression before OR after the pipe |
| 6 | Matches the literal character '6' |
| (?: | Start of non-capturing group |
| 0 | Matches the literal character '0' |
| 1 | Matches the literal character '1' |
| 1 | Matches the literal character '1' |
| | | Alternation — matches the expression before OR after the pipe |
| 5 | Matches the literal character '5' |
| \d | Matches any digit (0-9) |
| {2} | Matches exactly 2 times |
| ) | End of group |
| ) | End of group |
| [\s-] | Character class — matches any one of: \s- |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| {4} | Matches exactly 4 times |
| [\s-] | Character class — matches any one of: \s- |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| {4} | Matches exactly 4 times |
| [\s-] | Character class — matches any one of: \s- |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| {1,4} | Matches between 1 and 4 times |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates credit card number formats for major card networks. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2})) — A non-capturing group matching the first four digits, which identify the card network:
- 4\d{3} matches Visa cards (starting with 4)
- 5[1-5]\d{2} matches Mastercard (starting with 51-55)
- 3[47]\d{2} matches American Express (starting with 34 or 37)
- 6(?:011|5\d{2}) matches Discover (starting with 6011 or 65)
[\s-]? — An optional separator (space or hyphen) between digit groups. Cards are often displayed with separators for readability.
\d{4} — Matches four digits for the second group.
[\s-]? — Another optional separator.
\d{4} — Matches four digits for the third group.
[\s-]? — Another optional separator.
\d{1,4} — Matches one to four digits for the final group. Most cards have four digits here (16 total), but American Express has 15 digits total, and some other cards may vary.
$ — Anchors the match at the end.
This pattern validates the format and card network prefix but does not perform Luhn algorithm validation, which is the mathematical checksum used to verify card numbers. For complete validation, combine this regex with a Luhn check in your code.
Example Test Strings
| Input | Expected |
|---|---|
| 4111 1111 1111 1111 | Match |
| 5500-0000-0000-0004 | Match |
| 3782-8224-6310-005 | Match |
| 1234567890123456 | No Match |
| 6011000000000004 | Match |
Try It — Interactive Tester
86 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex