简体   繁体   English

文件扩展的正则表达式

[英]Regular Expression for Extension of File

I need 1 regular expression to put restrictions on the file types using it's extension. 我需要1个正则表达式来使用它的扩展名来限制文件类型。

I tried this for restricting the file type of html, .class, etc. 我试过这个来限制html,.class等的文件类型。

  1. /(\\.|\\/)[^(html|class|js|css)]$/i
  2. /(\\.|\\/)[^html|^class|^js|^css]$/i

I need to restrict a total of 10-15 types of files. 我需要限制总共10-15种类型的文件。 In my application there is a field for accepted file type, and according to the requirement I have file types which is to be restricted. 在我的应用程序中,有一个接受文件类型的字段,根据要求,我有要限制的文件类型。 So I need a regular expression using negation of restricted file type only. 所以我需要一个正则表达式,仅使用否定文件类型的否定。

The plugin code is like: 插件代码如下:

$('#fileupload').fileupload('option', {
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png|txt)$/i
});

I can specify the acceptedFileType but i have given the requirement to restrict a set of file. 我可以指定acceptedFileType但我已经给出了限制一组文件的要求。

Try /^(.*\\.(?!(htm|html|class|js)$))?[^.]*$/i 试试/^(.*\\.(?!(htm|html|class|js)$))?[^.]*$/i

Try it here: http://regexr.com?35rp0 在这里试试: http//regexr.com?35rp0

It will also work with extensionless files. 它也适用于无扩展文件。

As all the regexes, it's complex to explain... Let's start from the end 正如所有正则表达式一样,解释起来很复杂......让我们从最后开始

[^.]*$ 0 or more non . characters
( ... )? if there is something before (the last ?)

.*\.(?!(htm|html|class|js)$) Then it must be any character in any number .*
                             followed by a dot \.
                             not followed by htm, html, class, js (?! ... )
                             plus the end of the string $
                             (this so that htmX doesn't trigger the condition)

^ the beginning of the string

This one (?!(htm|html|class|js) is called zero width negative lookahead. It's explained at least 10 times every day on SO, so you can look anywhere :-) 这个(?!(htm|html|class|js)被称为零宽度负向前瞻。它在SO上每天至少解释10次,所以你可以看到任何地方:-)

You seem to misunderstand how a character class works. 您似乎误解了角色类的工作原理。 A character class matches only a single character. 字符类仅匹配单个字符。 The character chosen is the one from all of them in there. 选择的角色是那里的所有角色。 So, your character class: 那么,你的角色类:

[^(html|class|js|css)]

doesn't match html or class in sequence. 不符合htmlclass的顺序。 It just matches a single character out of all the distinct character in that class. 它只匹配该类中所有不同字符中的单个字符。

That said, for your particular task, you need to use negative look-ahead : 也就是说,对于您的特定任务,您需要使用负面预测

/(?!.*[.](?:html|class|js|css)$).*/

However, I would also consider using the String library in my respective language, instead of using regex, to achieve this task. 但是,我还会考虑使用我各自语言的String库,而不是使用正则表达式来完成此任务。 You just need to test, whether the string ends with any of those extension. 您只需要测试字符串是否以任何扩展名结尾。

If you are open to using JQuery, you might consider skipping the regex all together and going with an array of valid extensions instead: 如果您愿意使用JQuery,您可以考虑一起跳过正则表达式并改为使用有效扩展数组:

// store the file extensions (easy to maintain, if changesa are needed)
var aValidExtensions = ["htm", "html", "class", "js"];
// split the filename on "."
var aFileNameParts = file_name.split(".");

// if there are at least two pieces to the file name, continue the check
if (aFileNameParts.length > 1) {
    // get the extension (i.e., the last "piece" of the file name)
    var sExtension = aFileNameParts[aFileNameParts.length-1];

    // if the extension is in the array, return true, if not, return false
    return ($.inArray(sExtension, aValidExtensions) >= 0) ? true : false; 
}
else {
    return false;  // invalid file name format (no file extension)
}

The big advantage here is in the ease of maintenance . 这里的最大优点是易于维护。 . . changing acceptable file extensions is a quick update to the array (or even a property or CMS update, depending on how fancy things are :) ). 更改可接受的文件扩展名是对阵列的快速更新(甚至是属性或CMS更新,具体取决于花哨的东西:))。 Also, regex has a habit of being a little process intensive, so this should be more efficient (though, I haven't tested this particular case yet). 此外, regex有一个过程密集的习惯,所以这应该更有效(但是,我还没有测试过这个特例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM