BytePane

Credit Card Number Regex Pattern

Validates Visa, Mastercard, Amex, and Discover card numbers by their prefix patterns and lengths. Does not verify Luhn checksum.

#
Numbers
Advanced
Difficulty
Universal
Language
none
Flags
// Regular Expression
/^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$/

Live Regex Tester

Pattern Breakdown

^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$/;
const test = "4111111111111111";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$'
test = "4111111111111111"
match = re.search(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$`)
    fmt.Println(re.MatchString("4111111111111111")) // true
}

Common Use Cases

Payment form validationPCI compliancefraud detection

Match Examples

InputResult
4111111111111111Match
1234567890No Match

About the Credit Card Number Regex

Validates Visa, Mastercard, Amex, and Discover card numbers by their prefix patterns and lengths. Does not verify Luhn checksum.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The credit card number pattern is classified as advanced difficulty in the numbers category. It works in all major programming languages.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Credit Card Number regex pattern?

Validates Visa, Mastercard, Amex, and Discover card numbers by their prefix patterns and lengths. Does not verify Luhn checksum.

How do I use the Credit Card Number regex?

Use the pattern /^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$/ in your code. In JavaScript: new RegExp('^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$', ''). Test it above with your own input.

What does this Credit Card Number regex match?

This pattern matches: "4111111111111111". It does NOT match: "1234567890". Payment form validation, PCI compliance, fraud detection.

Is the Credit Card Number regex beginner-friendly?

This pattern is rated Advanced. It uses advanced features like lookaheads, backreferences, or complex alternation.

What languages support the Credit Card Number regex?

This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.

Can I modify the Credit Card Number regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools