Idea and implementation of getting file extensions in javascript

Source: Internet
Author: User

For a file name with an absolute path, such as: D: \ Program Files \ Notepad ++ \ notepad1_cmd.exe
First, to avoid escape backslash problems, you can use a regular expression to replace \ or \ with #, such:
D: # Program files?notepad={{notepad={.exe
Then, use '#' as the separator to break the string into an array and obtain the following array:
D: ProgramFiles Notepad ++ notepad1_cmd.exe
Take the last file name of the array with a suffix: notepad1_0000.exe
Separate the filename with a suffix into an array using the separator '.' to obtain the following array:
Notepad ++ exe
Then retrieve the last one of the array to get the file suffix exe.
The Code is as follows (Win7 + IE9 passed the test ):
Copy codeThe Code is as follows:
// By MoreWindows (http://www.jb51.net/qq)
Function GetExtensionFileName (pathfilename)
{
Var reg =/(\ +)/g;
Var pfn = pathfilename. replace (reg ,"#");
Var arrpfn = pfn. split ("#");
Var fn = arrpfn [arrpfn. length-1];
Var arrfn = fn. split (".");
Return arrfn [arrfn. length-1];
}
// By MoreWindows (http://www.jb51.net/qq)
Function GetExtensionFileName (pathfilename)
{
Var reg =/(\ +)/g;
Var pfn = pathfilename. replace (reg ,"#");
Var arrpfn = pfn. split ("#");
Var fn = arrpfn [arrpfn. length-1];
Var arrfn = fn. split (".");
Return arrfn [arrfn. length-1];
}

Test code:
Copy codeThe Code is as follows:
Function Test ()
{
Var filePath = "D :\\ Program Files \ Notepad ++ \ notepad1_.exe ";
Alert (GetExtensionFileName (filePath ));
}
<Input type = "button" value = "Test" onclick = "Test ()"/>
Function Test ()
{
Var filePath = "D :\\ Program Files \ Notepad ++ \ notepad1_.exe ";
Alert (GetExtensionFileName (filePath ));
}
<Input type = "button" value = "Test" onclick = "Test ()"/>

Click the "Test" button to bring up the "exe" dialog box, indicating that GetExtensionFileName can correctly parse the file name with an absolute path and get the suffix.
It is estimated that this method can only be used on the Windows platform. I don't know what it will do in Linux?

Related Article

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.