Credit Card Regex

After searching the internet for a credit card regex pattern, I found a lot of roundabout answers and no clear cut regex pattern. I aim to change that! Here is a regex pattern you can use to match credit cards (disclaimer: it may not work 100% for you but after a few tweaks I am sure it will!)

((?:(?:4\d{3})|(?:5[1-5]\d{2})|6(?:011|5[0-9]{2}))(?:-?|\040?)(?:\d{4}(?:-?|\040?)){3}|(?:3[4,7]\d{2})(?:-?|\040?)\d{6}(?:-?|\040?)\d{5})

If you have your own cool regex to share, post it in the comments! Hope this helps someone out there!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.