Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

javascript - Regular expression for excluding file types .exe and .js

I am using JQuery File upload plugin, for giving value to the option accept files I need the regular expression which will tells what are the file types to be restricted. I need to restrict both exe and js which i achieved by using below expression

(.|/)(!exe|!js)$

But this is expression was not allowing other files as well then I tried adding one extension as below

(.|/)(!exe|!js|pdf)$

With Above regular expression it is accepting only pdf and not accepting exe and JS. Now I need to enable for all file extentions except exe and js. It will be difficult to add all the extensions to the expression. Can we mention some how in the expression to accept other filetypes except exe and js in the similar format above. This regular expression is for JS.

Thanks,

Vinay

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...