Regex for credit card number
/\b(?:\d[ -]?){12,18}\d\b/g
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
\ba word boundary(?:a group, not capturing\da digit (0–9)[ -]?one of: a space or “-”, optionally){12,18}end of group, whole group between 12 and 18 times, as many as possible\da digit (0–9)\ba word boundary
Other patterns
- Email address
- URL
- IPv4 address
- UUID
- Hex color
- Date (YYYY-MM-DD)
- Time (24-hour)
- US phone number
- Phone number (E.164)
- US ZIP code
- Credit card number
- URL slug
- Username
- Strong password
- HTML tag
- Semantic version
- MAC address
- JSON Web Token
- Hashtag
- @mention
- Number
- Leading and trailing whitespace
- Repeated word
- Blank lines
- Markdown link
- Quoted string
- Domain name
- File extension
- Base64
- Trailing comma