jquery Gets the regular expression of the name of the uploaded file _jquery

Source: Internet
Author: User
Tags naming convention

Method One:

$ (' input[type= ' file '] '). On (' Change ', function () {
      var reg =/[^\\\/]*[\\\/]+/g;//Match the name and suffix of the file
      var name = $ (this). Val (). replace (Reg, ' ");
      var postfix =/\. [^\.] +/.exec (name),//Get the suffix
      of the file var text =name.substr (0,postfix[' index '));//Get name without suffix
      //detect uploaded file
    });

Method Two:

The following is just an example of a variable file that can be any string similar to a network path, an example of a file address that gets Name=file objects

Copy Code code as follows:

var file=jquery ("input[name= ' file ')"). Val ()
var filename=file.replace (/.* (\/|\\)/, "");
var fileext= (/[.] /.exec (filename))? /[^.] +$/.exec (Filename.tolowercase ()): ';

FileName gets filename

Fileext Get suffix Name

Method Three:

Most of the regular expressions found on the Internet do not take into account the Chinese folder and the Chinese filename.

Later, I wrote one by myself. After a lot of stray, I follow the Windows naming convention (the filename cannot contain the following characters://*? | "< >" Finally completes a regular formula that is considered to be more complete:

Copy Code code as follows:

([a-za-z]:) | (//)) (//) [^///*/?/|/: "<>]{1,255}) +/. (([j,j][p,p][g,g]) | ([g,g][i,i][f,f])

This expression is to determine whether the uploaded file type is jpg or GIF format. Test yourself, regardless of folder and filename is how to name, as long as you can find through Windows file management, can be correctly judged.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.