Regular expression for excluding file types .exe and .js

允我心安 提交于 2019-11-29 06:48:10

This will exclude .js and .exe at the end of the string, but allow anything else:

/^[^.]+$|\.(?!(js|exe)$)([^.]+$)/

Broken down:

  1. ^[^.]+$ matches any string with no dots
  2. \.(?!(js|exe)$)([^.]+$) matches a dot only if it is not followed by js or exe at the end of the string.

The following are allowed:

  • something.js.notjs
  • somethingelse.exee
  • /something.js/foo

The following are not allowed:

  • jquery.js
  • outlook.exe

Note: excluding certain file extensions is not a substitute for security, and even if it were JS and EXE files would not be a comprehensive blacklist. If your purpose in excluding certain extensions is to protect your server or your users, consider a white list of extensions and a thorough validation of file data after upload.

You're looking to use the (?!pattern) syntax. Also, while stating what you want to match, you aren't stating what should match. So this should do the trick:

(\.|\/)(?!exe|js).*$

This is saying "Match anything that's a dot (.) as long as it's not followed by 'exe' or 'js', and then match whatever you want after that."

I do not know this work or not but 99% this will work for you to exclude this exe or js extension use following pattern

/\.(exe|js)$/ig

Following code give you TRUE when file is on your blacklist. Otherwise give you FALSE

var result = /\.(exe|js)$/ig.test(filename);

Hope this help you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!