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?