-1

I'm trying to build a regex to meet the following requirement

4 digits / 6 digits / 3 digits

e.g. 4444/666666/333

But, the last slash and 3 digits are optional

Here is what i have so far:

/^\d{4}[/]\d{6}[/]{0,1}\d{0,3}$/

However, if i enter 4444/666666333 or 4444/666666/ that will also pass :(

Is it possible to put in a condition that will force a slash, if the remaining 3 digits are entered? Or, if the final slash is entered, the remaining 3 digits must also be entered too?

thanks Scott

Scott
  • 1,280
  • 4
  • 20
  • 35

2 Answers2

3

Try this pattern: ^\d{4}\/\d{6}(\/\d{3})?$.

Demo

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0

/^\d{4}[/]\d{6}([/]\d{0,3})?$/ use group () to include the last slash and 3 digits ,and use (?) to match it or no

Peng
  • 38
  • 4