I’m trying to write a javascript regexp for a blacklist of file extensions. I’m using a jquery plugin that has an option for acceptable file types that takes a regex, but rather than maintain a whitelist we would like to maintain a blacklist. So I need the regex to only match if the string doesn’t contain certain file extensions. Currently there is a regex we use to whitelist photo extensions on our photo upload:
/(\.|\/)(gif|jpe?g|png)$/i
For our document upload we would like to simply do a blacklist, but I haven’t been able to make the ?! delimeter work. So for the sake of an example how would I reverse this regex to match as long as the file extension doesn’t contain gif, jpg, jpeg, png?
I’ve tried several different ways of using the ?!, but nothing I’ve tried has worked properly. Heres some examples of what I have tried unsuccessfully:
/(\.|\/)(?!gif|jpe?g|png)$/i
/(\.|\/)(?!(gif|jpe?g|png))$/i
Essentially I need this regex to always return true unless the blacklisted file extensions are matched.
This works:
I think it’s because the “match only if not followed by” doesn’t like parentheses before it, but I’m not sure.
This new regex now works with the parentheses and multiple extensions:
However, to allow files with no extension, this must be used:
Looking at the complexity of that regex, I suggest that you implement something server-side or use another plugin.