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 |
是 | 是 | 是 | 是 | 是 | 是 |