文件扩展的正则表达式

iRu*_*ner 5 javascript regex

我需要1个正则表达式来使用它的扩展名来限制文件类型.

我试过这个来限制html,.class等的文件类型.

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

我需要限制总共10-15种类型的文件.在我的应用程序中,有一个接受文件类型的字段,根据要求,我有要限制的文件类型.所以我需要一个正则表达式,仅使用否定文件类型的否定.

插件代码如下:

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

我可以指定acceptedFileType但我已经给出了限制一组文件的要求.

xan*_*tos 16

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

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

它也适用于无扩展文件.

正如所有正则表达式一样,解释起来很复杂......让我们从最后开始

[^.]*$ 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
Run Code Online (Sandbox Code Playgroud)

这个(?!(htm|html|class|js)被称为零宽度负向前瞻.每天至少解释10次SO,所以你可以随处看看:-)