简体   繁体   中英

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

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?

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])$

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM