Barış Velioğlu
Barış Velioğlu

Reputation: 5827

Regex character limit with specified numbers

I want to accept a number if it is length of 7, 9 or 10. Is there any short version of it ?

I can limit max and min length like below , but is there any way to make this range fixed by specified numbers.

[\d]{7,10}$

Upvotes: 0

Views: 128

Answers (1)

Matti Virkkunen
Matti Virkkunen

Reputation: 65176

Well, you can get rid of the [] but that's about as short as it gets.

^(\d{7}|\d{9}|\d{10})$

Alternatively you could do this which does save a few characters but I think the first one is easier to read:

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

Upvotes: 5

Related Questions