JavaScript String lastIndexOf() method
- 上一页 indexOf()
- 下一页 length
- 返回上一层 Kita Yarika JavaScript String
definition and usage
lastIndexOf()
method returns the index (subscript) of the last occurrence of the specified value in the string.
lastIndexOf()
method searches the string from the beginning to the end.
lastIndexOf()
method returns the index from the beginning (position 0).
if the value is not found, then lastIndexOf()
method returns -1
.
lastIndexOf()
method is case-sensitive.
see also:
instance
Example 1
Search for the last occurrence of "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");
Example 2
Search for the last occurrence of "planet" starting from position 20:
let text = "Hello planet earth, you are a great planet."; let result = text.lastIndexOf("planet", 20);
syntax
string.lastIndexOf(substring, start)
parameter
parameter | description |
---|---|
substring | required. The string to be searched. |
start |
optional. Starting position. default value is the length of the string. |
return value
type | description |
---|---|
number |
search value position. if it does not appear, then return |
technical details
return value
if string in start position before there is substringthen return the last substring position. If the substring is not found substringthen return -1.
description
lastIndexOf()
method searches the string string from the end to the beginning to see if it contains the substring substring. The starting position of the search is in the string string the start or string end (not specified start when a substringthen the lastIndexOf() method will return substring the first character in string because it searches the string from the end to the beginning, so the first substring in fact string position in start the last substring.
if string no substringthen the method returns -1.
Note:although lastIndexOf()
method is to search the string from the end to the beginning, but the character position it returns is still calculated from the beginning. The position of the first character in the string is 0, and the position of the last character is string.length-1.
browser support
lastIndexOf()
is ECMAScript1 (ES1) feature.
All browsers fully support ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 | 支持 |
- 上一页 indexOf()
- 下一页 length
- 返回上一层 Kita Yarika JavaScript String