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?

Was it helpful?

Solution

Another option would be

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

regex101 demo

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top