简体   繁体   中英

Regular Expression for Extension of File

I need 1 regular expression to put restrictions on the file types using it's extension.

I tried this for restricting the file type of html, .class, etc.

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

I need to restrict a total of 10-15 types of files. 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.

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

Try it here: 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 :-)

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. 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. 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:

// 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 :) ). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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