ITeye博客

利用lastIndexOf() 获取文件后缀名

小丑鱼0703 · 2015-03-30 14:51 · js-String · Web前端 · Markdown

lastIndexOf() 方法 返回指定值在调用该方法的字符串中最后出现的位置,如果没找到则返回 -1。从该字符串的后面向前查找,从 fromIndex 处开始。 字符串中的字符被从左向右索引。首字符的索引(index)是 0,最后一个字符的索引是 stringName.length - 1。


"aa.txt".lastIndexOf(".");//2

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf

获取文件后缀名函数:


function catchtype(source){  
     var typestr = source.toLowerCase().substring(source.lastIndexOf('.')+1); 
     return typestr;
}
catchtype("aa.txt");//"txt"

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。