JavaScript String lastIndexOf() 方法
示例
搜索“planet”的最后一次出现
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("planet");
自己动手试一试 »
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("Planet");
自己动手试一试 »
更多示例见下文。
描述
lastIndexOf()
方法返回指定值在字符串中最后一次出现的位置(索引)。
lastIndexOf()
方法从字符串的末尾开始搜索。
lastIndexOf()
方法从字符串开头(位置 0)返回索引。
如果找不到该值,lastIndexOf()
方法返回 -1。
lastIndexOf()
方法区分大小写。
另请参阅
语法
string.lastIndexOf(searchvalue, start)
参数
参数 | 描述 |
searchvalue | 必需。 要搜索的字符串。 |
start | 可选。 搜索开始的位置。 默认值为字符串长度。 |
返回值
类型 | 描述 |
一个数字 | 搜索值出现的位置。 如果从未出现,则为 -1。 |
更多示例
从位置 20 开始搜索“planet”的最后一次出现
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("planet", 20);
自己动手试一试 »
浏览器支持
lastIndexOf()
是 ECMAScript1 (ES1) 功能。
ES1 (JavaScript 1997) 在所有浏览器中都得到完全支持
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |