---
title: "利用lastIndexOf() 获取文件后缀名"
author: "小丑鱼0703"
date: "2015-03-30T14:51:43Z"
tags: []
categories: ["Web前端"]
slug: "lastindexof"
draft: false
lastmod: "2015-03-30T14:54:02Z"
id: 2197087
url: "https://wangjingyi.iteye.com/blog/2197087"
---

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

```js

"aa.txt".lastIndexOf(".");//2
```


<https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf>

获取文件后缀名函数：

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