Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2022-W00

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

Any idea how to fix it?

>Solution :

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])$
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading