博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在JavaScript中获取文件扩展名?
阅读量:2518 次
发布时间:2019-05-11

本文共 1038 字,大约阅读时间需要 3 分钟。

How to get file extension from a file name in ?

如何从的文件名获取文件扩展名?

For examples,

举些例子,

For “file.txt”, I want to get “txt”.

For “file2.multi.ext.fileext”, I want to get “fileext”.

对于“ file.txt”,我想获取“ txt”。

对于“ file2.multi.ext.fileext”,我想获取“ fileext”。

This JavaScript function works well for me.

这个JavaScript函数对我来说效果很好。

function getExt(filename){    var idx = filename.lastIndexOf('.');    // handle cases like, ., filename    return (idx < 1) ? "" : filename.substr(idx + 1);}

It handles situations that the filename has no extensions like “.htaccess” and “filename”.

它可以处理文件名没有扩展名(如“ .htaccess”和“ filename”)的情况。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。


//This is fine if you know that there will be a nothing but a file name in "fileName" function getExt(fileName){     return (fileName.lastIndexOf('.') < 1) ?   null : fileName.('.').slice(-1);}//this is better for unexpected path data like: "/weird.folder/somefolder"function getExt(path){    return (path.match(/(?:.+..+[^\/]+$)/ig) != null) ? path.split('.').slice(-1): 'null';}

翻译自:

转载地址:http://wqlwd.baihongyu.com/

你可能感兴趣的文章
PAT乙级1014
查看>>
ORACLE wm_concat自定义
查看>>
[Zend PHP5 Cerification] Lectures -- 6. Database and SQL
查看>>
[Drupal] Using the Administrator theme whenever you want.
查看>>
【Hibernate框架】关联映射(一对一关联映射)
查看>>
【算法】大数乘法
查看>>
WPF解析PPT为图片
查看>>
JavaScrict中的断言调试
查看>>
密码服务
查看>>
结构体在内存中的存储
查看>>
冲刺阶段—个人工作总结01
查看>>
基于Python的Webservice开发(二)-如何用Spyne开发Webservice
查看>>
PowerDesigner修改设计图中文字的字体大小等样式
查看>>
Python list和 np.Array 的转换关系
查看>>
jenkins忘记密码如何处理?
查看>>
布尔操作符-逻辑或(||)
查看>>
vim的列编辑操作
查看>>
Linux驱动学习 —— 在/sys下面创建目录示例
查看>>
Linux下安装Android的adb驱动-解决不能识别的问题
查看>>
Why is the size of an empty class not zero in C++?
查看>>