0

I need a regular expression for date format: mm/yyyy in Javascript.

I try by the following.

var reDate = /^(\d{1,2})/(\d{4})$/;

But not working. I think the problem is in /

2
  • 1
    That's not regex, that is a String. Use var reDate = /^(\d{1,2})-(\d{4})$/; Commented Nov 30, 2015 at 3:31
  • 1
    You need a RegExp object, var rgx = new RegExp(reDate); Commented Nov 30, 2015 at 3:33

1 Answer 1

5

To use forward slash in regex, it need to be escaped, because it is also used as delimiter.

/^(\d{1,2})\/(\d{4})$/
           ^^

The above regex will also match any two digits, ex. 00, 99. Use following regex to match digits only from 1-12.

Credits to How can I use a regular expression to validate month input?

^(0?[1-9]|1[012])\/[0-9]{4}$

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Aso matches "0/2015/" /^(\d{1,2})\/(\d{4})$/.test("0/2011");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.