用于排除文件类型.exe和.js的正则表达式 [英] Regular expression for excluding file types .exe and .js

查看:736
本文介绍了用于排除文件类型.exe和.js的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery File上传插件,为选项接受文件赋值我需要正则表达式,它将告诉要限制的文件类型是什么。我需要通过使用以下表达式来限制我实现的exe和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)$

使用Above正则表达式,它只接受pdf而不接受exe和JS。现在我需要启用除exe和js之外的所有文件扩展名。将所有扩展添加到表达式将很困难。我们可以在表达式中提到一些如何接受除上面类似格式的exe和js之外的其他文件类型。这个正则表达式适用于JS。

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.

谢谢,

Vinay

推荐答案

这将排除 .js .exe at字符串的结尾,但允许其他任何内容:

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

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

细分:


  1. ^ [^。] + $ 匹配任何没有点的字符串

  2. \。(?!(js | exe)$)([^。] + $)仅在未遵循点时匹配点通过 js exe 在字符串的末尾。

  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.

允许以下内容:


  • something.js.notjs

  • somethingelse.exee

  • / something.js / foo

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

以下是不允许的:


  • jquery.js

  • outlook.exe

  • jquery.js
  • outlook.exe

注意:排除某些文件扩展名不能替代安全性,即使它是JS和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.

这篇关于用于排除文件类型.exe和.js的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆