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

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
(?:Start of non-capturing group
4Matches the literal character '4'
\dMatches any digit (0-9)
{3}Matches exactly 3 times
|Alternation — matches the expression before OR after the pipe
5Matches the literal character '5'
[1-5]Character class — matches any one of: 1-5
\dMatches any digit (0-9)
{2}Matches exactly 2 times
|Alternation — matches the expression before OR after the pipe
3Matches the literal character '3'
[47]Character class — matches any one of: 47
\dMatches any digit (0-9)
{2}Matches exactly 2 times
|Alternation — matches the expression before OR after the pipe
6Matches the literal character '6'
(?:Start of non-capturing group
0Matches the literal character '0'
1Matches the literal character '1'
1Matches the literal character '1'
|Alternation — matches the expression before OR after the pipe
5Matches the literal character '5'
\dMatches 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)
\dMatches 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)
\dMatches 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)
\dMatches 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

InputExpected
4111 1111 1111 1111Match
5500-0000-0000-0004Match
3782-8224-6310-005Match
1234567890123456No Match
6011000000000004Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 86 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →