Home  >  Article  >  Web Front-end  >  The idea and implementation of getting the file suffix name in javascript_javascript skills

The idea and implementation of getting the file suffix name in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:29:161258browse

For a file name with an absolute path, such as: D:Program FilesNotepad Notepad .exe
First of all, in order to avoid problems with escaping backslashes, you can use regular expressions to replace or with #, such as:
D: #Program Files#Notepad #Notepad .exe
After that, use '#' as the delimiter to decompose the string into an array and get the following array:
D: ProgramFiles Notepad Notepad .exe
Get the last one of the array That is the file name with the suffix: Notepad .exe
and then use '.' as the delimiter to decompose the file name with the suffix into an array, and get the following array:
Notepad exe
and then take the array The last one can get the file suffix exe
The code is as follows (Win7 IE9 test passed):

Copy the code The 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 code The code is as follows:

function Test()
{
var filePath="D:\Program Files\Notepad \Notepad .exe";
alert(GetExtensionFileName(filePath));
}

function Test()
{
var filePath="D:\Program Files\ Notepad \Notepad .exe";
alert(GetExtensionFileName(filePath));
}


Click the Test button to pop up a dialog box with exe content, indicating that GetExtensionFileName can correctly parse the file name with an absolute path and obtain the suffix name.
I guess this method can only be used on the Windows platform. I wonder what will happen if it is executed on Linux?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn