I use this javascript str.match(regex)
REgex is
/(?!(?:2020|2021)$)\d{4,5}/g
Test text
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

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