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

javascript regex extract ONLY 4 or 5 digits numbers except

I use this javascript str.match(regex)

REgex is

/(?!(?:2020|2021)$)\d{4,5}/g

Test text

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

12345
R411186062 In 2020 voice nr: 26324 htt ps:// site.com
123456202077
<p style="font-weight: 400;">„Pour Elle“
56542

I need extract only 4 or 5 exactly digits from string except numbers 2020 and 2021, but this regex extract part of numbers which have more than 5 digits.

Result:

12345, 41118, 6062, 2020, 26324, 12345, 62020, 56542

enter image description here
Expected result:
12345, 26324, 56542

How to fix regex?

>Solution :

You can use word boundary for that

\b\d{4,5}\b

https://regex101.com/r/16ebJ7/1

This will give you the expected results

EDIT

To exclude 2020 and 2021 you can use a negative look ahead

(?!2020|2021)\b\d{4,5}\b
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