The thought of JavaScript getting file suffix name and the technique of realizing _javascript

Source: Internet
Author: User
Tags arrays
For a file name with an absolute path, such as: D:\Program Files\notepad++\notepad++.exe
First, to avoid the escape of the backslash, you can replace \ or \ \ with a regular expression with a #, such as:
D: #Program files#notepad++ #Notepad ++.exe
The string is then decomposed into arrays of ' # ', and the following array is obtained:
D:programfiles notepad++ Notepad++.exe
The last one to take the array is the filename with the suffix: notepad++.exe
And then the '. ' As a delimiter, the filename with the suffix is decomposed into arrays, and the following array is obtained:
notepad++ exe
And then take the last one of the array to get the file's suffix name exe.
The code is as follows (WIN7+IE9 test passed):
Copy Code code 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 Code code as follows:

function Test ()
{
var filepath= "D:\\Program Files\\notepad++\\notepad++.exe";
Alert (Getextensionfilename (FilePath));
}
<input type= "button" value= "Test" onclick= "Test ()"/>
function Test ()
{
var filepath= "D:\\Program Files\\notepad++\\notepad++.exe";
Alert (Getextensionfilename (FilePath));
}
<input type= "button" value= "Test" onclick= "Test ()"/>

Click on the test button to eject the contents of the EXE dialog box, indicating that getextensionfilename can correctly resolve the file name with an absolute path and get the suffix name.
It is estimated that this method can only be used under the Windows platform, under the Linux implementation do not know how?
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.