lastIndexOf()
方法返回索引在调用
String
object of the last occurrence of the specified value, searching backwards from
fromIndex
。返回
-1
若未找到值。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
str.lastIndexOf(searchValue[, fromIndex])
searchValue
searchValue
is an empty string, then
fromIndex
被返回。
fromIndex
可选
+Infinity
. If
fromIndex
>=
str
.length
, the whole string is searched. If
fromIndex
< 0
, the behavior will be the same as if it would be
0
.
The index of the last occurrence of
searchValue
;
-1
若找不到。
Characters in a string are indexed from left to right. The index of the first character is
0
, and the index of the last character is
str
.length - 1
.
'canal'.lastIndexOf('a'); // returns 3
'canal'.lastIndexOf('a', 2); // returns 1
'canal'.lastIndexOf('a', 0); // returns -1
'canal'.lastIndexOf('x'); // returns -1
'canal'.lastIndexOf('c', -5); // returns 0
'canal'.lastIndexOf('c', 0); // returns 0
'canal'.lastIndexOf(''); // returns 5
'canal'.lastIndexOf('', 2); // returns 2
注意:
'abab'.lastIndexOf('ab', 2)
will return
2
and not
0
, as
fromIndex
limits only the beginning of the match.
lastIndexOf()
method is case sensitive. For example, the following expression returns
-1
:
'Blue Whale, Killer Whale'.lastIndexOf('blue'); // returns -1
indexOf()
and
lastIndexOf()
以下范例使用
indexOf()
and
lastIndexOf()
to locate values in the string
"Brave new world"
.
let anyString = 'Brave new world';
console.log('The index of the first w from the beginning is ' + anyString.indexOf('w'));
// logs 8
console.log('The index of the first w from the end is ' + anyString.lastIndexOf('w'));
// logs 10
console.log('The index of "new" from the beginning is ' + anyString.indexOf('new'));
// logs 6
console.log('The index of "new" from the end is ' + anyString.lastIndexOf('new'));
// logs 6
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.lastIndexOf' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
lastIndexOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 6 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
String.prototype.charAt()
String.prototype.indexOf()
String.prototype.split()
Array.prototype.indexOf()
Array.prototype.lastIndexOf()
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()