Home > Blockchain >  Regex match optional character
Regex match optional character

Time:06-13

Regex define: YYYY-NNN/EXP

  • YYYY: exactly 4 number 0000 -> 9999
  • NNN: exactly 3 number 000 -> 999
  • /: optional
  • EXP: any character, optional

My regex: /^(\d{4})-(\d{3})(\/)?([\s\S]*)$/

I want to value exactly 2022-001 or 2022-001/anything. But I check value is 2022-001fsd, it match.

Is there anyway regex value exactly 2022-001 or 2022-001/anything?

Thanks!

CodePudding user response:

You need to make the trailing portion of your pattern optional using the ? character, e.g.

^\d{4}-\d{3}(?:/\S )?$

Demo

  • Related