Credit Card Number Regex

Matches major credit card numbers (Visa, Mastercard, Amex) with or without dashes.

パターン

/(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})/g
→ ビジュアライザーで開く

テスト例

Visa: 4111111111111111, Amex: 371449635398431

コード例

javascript

const regex = /(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})/g;
const result = str.match(regex);

python

import re
pattern = re.compile(r'(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})')
result = pattern.findall(text)

go

import "regexp"
re := regexp.MustCompile(`(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})`)
result := re.FindAllString(text, -1)
validationcredit cardpayment