user1485864
user1485864

Reputation: 519

Javascript regex: get all numbers having between 2 and 6 digits but not 5

I would like to write a regex that check if the input string is a number between 2 and 6 digits but not 5. Obviously, I can do something like:

^[0-9]{2,4}$|^[0-9]{6}$

but I was hoping for more succinct notation.

This is JavaScript regex. Do you think there is a shorter answer?

Many thanks!

Upvotes: 2

Views: 117

Answers (3)

Paul Roub
Paul Roub

Reputation: 36438

A more-succint version of the same logic:

^(\d{2,4}|\d{6})$

Upvotes: 3

vks
vks

Reputation: 67968

^(?!.{5}$)\d{2,6}$

Try this.See demo.

http://regex101.com/r/yW4aZ3/116

Upvotes: 1

anubhava
anubhava

Reputation: 785316

You can use this:

^(\d{2,3}|\d{4}(\d{2})?)$

RegEx Demo

Upvotes: 0

Related Questions