Sunday, January 29, 2012

Exclude a certain number from this regex


I have this regex that test for an input with max length of 5. My problem is I want to exclude the single digit of 2. If string contains only number "2", it should fail. How do I exclude number 2 in this regex? /^([a-zA-Z,\d]){1,5}$/







13425 - Match

03277 - Match

2 - Fail.

1 comment:

  1. A negative look-ahead assertion can do this for you

    /^(?!2$)([a-zA-Z,\d]){1,5}$/

    ReplyDelete