-1

I am trying to write the regex for CC-YY-NNNN(PE-19-001). Where CC is a character, YY is the year NNNN could be a 3 or a 4 digit positive number. There will be exactly two dashes in the string.

So far, what I have tried is :

if( value.includes("-") && /\d/.test(value) && /[a-zA-Z]/.test(value) && /-\d$/.test('-1') )

But I am unable to get any close.

1 Answers1

0

You could use a single pattern to match 2 uppercase chars [A-Z], 2 digits and 3-4 digits using a quantifier {3,4}

^[A-Z]{2}-\d{2}-\d{3,4}$

Regex demo

const regex = /^[A-Z]{2}-\d{2}-\d{3,4}$/;
[
  "PE-19-001",
  "PE-19-0011",
  "P-19-001",
  "PE-1-001"
].forEach(s => console.log(s + " --> " + regex.test(s)))
The fourth bird
  • 96,715
  • 14
  • 35
  • 52