I need to extract numbers that are longer than 3 digits and do not include years within a given range (e.g. between 19xx and 2020, where XX is always in the end of the string).
I am currently using the following pattern:
/(?!19[0-9]{2}|200[0-9]|201[0-9]|202[0-9])\d{3,}$/i
When I test the expression with “something 2012”, I always get the result 012. I need to get null.
var s = "moose high performance drive belt 2012";
s.match(/(?!19[0-9]{2}|200[0-9]|201[0-9]|202[0-9])\d{3,}$/i);
Why does this expression incorrectly match the end of a date?
It discards
something, then attempts to match2012but fails due to your negative look-ahead assertion, then attempts to match012, which succeeds because indeed,012does not match your negative lookahead assertion.UPDATE:
This isn’t pretty but it’s one solution. Perhaps you can simplify it.
See a demo here: http://rubular.com/r/FLiehrUEp8.