Home > Net >  regex js how to to exclude from a match two consecutive zeros?
regex js how to to exclude from a match two consecutive zeros?

Time:03-08

I have the following regular expression

^[2-9][0-9]{3}-W([0-5][0-3]|(?!00))

https://regex101.com/r/OOCxzT/1

I need to exclude from the match a string with two consecutive 00 for instance

2022-W00

I added (?!00) but does not seem to work properly.

Any idea how to fix it?

CodePudding user response:

Using the negative lookahead in the alternation ([0-5][0-3]|(?!00)) this part (?!00) will be true (giving you a partial match) if the first alternative does not match, and from the current position what is directly to the right is not 00

The lookahead should be right before the second match of the digits.

^[2-9][0-9]{3}-W(?!00)([0-5][0-3])

Regex demo

If you only want to match 2 digits, you can also append an anchor $

^[2-9][0-9]{3}-W(?!00)([0-5][0-3])$

CodePudding user response:

Is this what you need ? or more digit after 'W' ?

^[2-9][0-9]{3}-W([0-5][1-3]|[1-5][0-3])
  • Related