Regular expression to match credit card expiration date

regex

Solution

You're missing start of line anchor `^` and parenthesis are unmatched.

This should work:

re = /^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/;

OR using word boundaries:

re = /\b(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})\b/;

Working Demo: http://regex101.com/r/gN5wH2

Problem

I have the following pattern which I'm trying to use to match credit card expiration dates: ``` (0[1-9]|1[0-2])\/?(([0-9]{4})|[0-9]{2}$) ``` and I'm testing on the following strings: ``` 02/13 0213 022013 02/2013 02/203 02/2 02/20322 ``` It should only match the first four strings, and the last 3 should not be a match as they are invalid. However the current pattern is also matching the last string. What am I doing wrong?

Original source

Related problems