Question

When I want to test if input string is at least length 10 and at most length 13, I use this pattern: [0-9]{10,13}

But how would you test if the string length is 10 or 13 and nothing in between?

EDIT: I see two dominant solutions:

1) Using length1 OR length2 2) Using length1 + optional characters of (length2 - length1)

Just wondering: are there any performance differences between the two?

Était-ce utile?

La solution

Another option would be

^[0-9]{10}(?:[0-9]{3})?$

regex101 demo

Autres conseils

use this expression ^((?:\d{3}){3,4}\d)$

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top