
- Javascript 基礎教程
- Javascript - 首頁
- JavaScript - 路線圖
- JavaScript - 概述
- JavaScript - 特性
- JavaScript - 啟用
- JavaScript - 位置
- JavaScript - 語法
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - 註釋
- JavaScript - 變數
- JavaScript - let 語句
- JavaScript - 常量
- JavaScript - 資料型別
- JavaScript - 型別轉換
- JavaScript - 嚴格模式
- JavaScript - 保留關鍵字
- JavaScript 運算子
- JavaScript - 運算子
- JavaScript - 算術運算子
- JavaScript - 比較運算子
- JavaScript - 邏輯運算子
- JavaScript - 位運算子
- JavaScript - 賦值運算子
- JavaScript - 條件運算子
- JavaScript - typeof 運算子
- JavaScript - 空值合併運算子
- JavaScript - delete 運算子
- JavaScript - 逗號運算子
- JavaScript - 分組運算子
- JavaScript - yield 運算子
- JavaScript - 展開運算子
- JavaScript - 指數運算子
- JavaScript - 運算子優先順序
- JavaScript 控制流
- JavaScript - if...else
- JavaScript - while 迴圈
- JavaScript - for 迴圈
- JavaScript - for...in
- Javascript - for...of
- JavaScript - 迴圈控制
- JavaScript - break 語句
- JavaScript - continue 語句
- JavaScript - switch case
- JavaScript - 使用者自定義迭代器
- JavaScript 函式
- JavaScript - 函式
- JavaScript - 函式表示式
- JavaScript - 函式引數
- JavaScript - 預設引數
- JavaScript - Function() 建構函式
- JavaScript - 函式提升
- JavaScript - 自執行函式
- JavaScript - 箭頭函式
- JavaScript - 函式呼叫
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - 閉包
- JavaScript - 變數作用域
- JavaScript - 全域性變數
- JavaScript - 智慧函式引數
- JavaScript 物件
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - String
- JavaScript - Array
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Set
- JavaScript - WeakSet
- JavaScript - Map
- JavaScript - WeakMap
- JavaScript - Iterable
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - 模板字面量
- JavaScript - 標籤模板
- 面向物件的 JavaScript
- JavaScript - 物件
- JavaScript - 類
- JavaScript - 物件屬性
- JavaScript - 物件方法
- JavaScript - 靜態方法
- JavaScript - 顯示物件
- JavaScript - 物件訪問器
- JavaScript - 物件建構函式
- JavaScript - 原生原型
- JavaScript - ES5 物件方法
- JavaScript - 封裝
- JavaScript - 繼承
- JavaScript - 抽象
- JavaScript - 多型
- JavaScript - 解構賦值
- JavaScript - 物件解構
- JavaScript - 陣列解構
- JavaScript - 巢狀解構
- JavaScript - 可選鏈
- JavaScript - 全域性物件
- JavaScript - Mixin
- JavaScript - Proxy
- JavaScript 版本
- JavaScript - 歷史
- JavaScript - 版本
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript 非同步
- JavaScript - 非同步
- JavaScript - 回撥函式
- JavaScript - Promise
- JavaScript - Async/Await
- JavaScript - 微任務
- JavaScript - Promise 化
- JavaScript - Promise 鏈式呼叫
- JavaScript - 定時事件
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookie
- JavaScript - Cookie
- JavaScript - Cookie 屬性
- JavaScript - 刪除 Cookie
- JavaScript 瀏覽器 BOM
- JavaScript - 瀏覽器物件模型
- JavaScript - Window 物件
- JavaScript - Document 物件
- JavaScript - Screen 物件
- JavaScript - History 物件
- JavaScript - Navigator 物件
- JavaScript - Location 物件
- JavaScript - Console 物件
- JavaScript Web API
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript 事件
- JavaScript - 事件
- JavaScript - DOM 事件
- JavaScript - addEventListener()
- JavaScript - 滑鼠事件
- JavaScript - 鍵盤事件
- JavaScript - 表單事件
- JavaScript - Window/Document 事件
- JavaScript - 事件委託
- JavaScript - 事件冒泡
- JavaScript - 事件捕獲
- JavaScript - 自定義事件
- JavaScript 錯誤處理
- JavaScript - 錯誤處理
- JavaScript - try...catch
- JavaScript - 除錯
- JavaScript - 自定義錯誤
- JavaScript - 擴充套件錯誤
- JavaScript 重要關鍵字
- JavaScript - this 關鍵字
- JavaScript - void 關鍵字
- JavaScript - new 關鍵字
- JavaScript - var 關鍵字
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM 方法和屬性
- JavaScript - DOM Document
- JavaScript - DOM 元素
- JavaScript - DOM 屬性 (Attr)
- JavaScript - DOM 表單
- JavaScript - 修改 HTML
- JavaScript - 修改 CSS
- JavaScript - DOM 動畫
- JavaScript - DOM 導航
- JavaScript - DOM 集合
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript 其他
- JavaScript - Ajax
- JavaScript - 非同步迭代
- JavaScript - Atomics 物件
- JavaScript - rest 引數
- JavaScript - 頁面重定向
- JavaScript - 對話方塊
- JavaScript - 頁面列印
- JavaScript - 驗證
- JavaScript - 動畫
- JavaScript - 多媒體
- JavaScript - 圖片地圖
- JavaScript - 瀏覽器
- JavaScript - JSON
- JavaScript - 多行字串
- JavaScript - 日期格式
- JavaScript - 獲取日期方法
- JavaScript - 設定日期方法
- JavaScript - 模組
- JavaScript - 動態匯入
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - 淺複製
- JavaScript - 呼叫棧
- JavaScript - 引用型別
- JavaScript - IndexedDB
- JavaScript - 點選劫持攻擊
- JavaScript - 柯里化
- JavaScript - 圖形
- JavaScript - Canvas
- JavaScript - 防抖
- JavaScript - 效能
- JavaScript - 樣式指南
JavaScript 字串 lastIndexOf() 方法
JavaScript 字串 lastIndexOf() 方法用於查詢指定子字串在原始字串中最後一次出現的索引。它返回子字串的位置,如果子字串不存在則返回 -1。例如,“hello”.lastIndexOf("ll") 返回 2,而 “hello”.lastIndexOf("hi") 返回 -1。
此方法接受一個名為 position 的可選引數,該引數指定方法從原始字串中的哪個位置開始搜尋指定的子字串。此引數的預設值為 0。例如,“hello”.lastIndexOf("l", 2) 返回 2,而 “hello”.lastIndexOf("l", 3) 返回 3。
注意 - 此方法區分大小寫,它將“a”和“A”視為不同的值。
語法
以下是 JavaScript 字串 lastIndexOf() 方法的語法:
lastIndexOf(searchString, position)
引數
- searchString - 要搜尋的子字串。
- position (可選) - 從哪個位置開始搜尋。
返回值
此方法返回找到的子字串最後一次出現的索引。
示例 1
如果我們省略 position 引數,此方法將為 position 考慮預設值 0,並返回指定子字串“t”在原始字串“TutorialsPoint”中最後一次出現的索引。
<html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <script> const str = "TutorialsPoint"; const sub_str = "t"; document.write("Original string: ", str); document.write("<br>Sub-string to be searched: ", sub_str); document.write("<br>An index of search string '", sub_str,"' is: ", str.lastIndexOf(sub_str)); </script> </body> </html>
輸出
上述程式返回子字串“t”的索引為 13。
Original string: TutorialsPoint Sub-string to be searched: t An index of search string 't' is: 13
示例 2
如果我們將 position 引數值設定為 5,則該方法將從原始字串中的位置 5 開始搜尋子字串。
以下是 JavaScript 字串 lastIndexOf() 方法的另一個示例。在這裡,我們使用此方法來查詢子字串“o”在原始字串“HelloWorld”中起始位置為 5 的索引。
<html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <script> const str = "HelloWorld"; const sub_str = "o"; const position = 5; document.write("Original string: ", str); document.write("<br>Sub-string to be searched: ", sub_str); document.write("<br>Position value is: ", position); document.write("<br>An index of search string '", sub_str,"' is: ", str.lastIndexOf(sub_str, position)); </script> </body> </html>
輸出
執行上述程式後,它將返回在指定位置 5 處子字串“o”的索引值為“4”。
Original string: HelloWorld Sub-string to be searched: o Position value is: 5 An index of search string 'o' is: 4
示例 3
如果在原始字串中找不到指定的子字串,此方法將返回 -1。
在下面的示例中,我們嘗試在原始字串“JavaScript”中查詢子字串“Mom”的索引。
<html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <script> const str = "JavaScript"; const sub_str = "Mom"; document.write("Original string: ", str); document.write("<br>Sub-string to be searched: ", sub_str); let result = str.lastIndexOf(sub_str); document.write("<br>Method str.lastIndexOf(sub_str) returns: ", result); if(result == -1){ document.write("<br>Not found...!"); } else{ document.write("<br>Found...!"); } </script> </body> </html>
輸出
上述程式中的 lastIndexOf() 方法在找不到子字串時返回 -1。
Original string: JavaScript Sub-string to be searched: Mom Method str.lastIndexOf(sub_str) returns: -1 Not found...!
示例 4
讓我們透過執行以下程式來了解 indexOf() 和 lastIndexOf() 方法的區別。
<html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <script> const str = "JavaScript"; const sub_str = "a"; const position = 20; document.write("Original string: ", str); document.write("<br>Sub-string to be searched: ", sub_str); document.write("<br>Position value: ", position); let result = str.lastIndexOf(sub_str, position); document.write("<br>Method str.lastIndexOf(sub_str) returns: ", result); </script> </body> </html>
輸出
對於相同的子字串,indexOf() 和 lastIndexOf() 方法返回不同的索引值,如下所示:
Original string: JavaScript Sub-string to be searched: a Position value: 20 Method str.lastIndexOf(sub_str) returns: 3