Frage

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?

War es hilfreich?

Lösung

Another option would be

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

regex101 demo

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top