此方法調用String對象之內返回索引指定的值最後一次出現,開始搜索在的fromIndex或如果沒有找到該值則返回-1。
語法
string.lastIndexOf(searchValue[, fromIndex])
下麵是參數的詳細資訊:
-
searchValue : 一個字串,表示要搜索的值
-
fromIndex : 在調用字串內的位置,從開始搜索。它是介於0-字串的長度任意整數。缺省值是0。
返回值:
返回最後出現的索引,如果沒有找到則為-1
例子:
<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
document.write("lastIndexOf found String :" + index );
document.write("<br />");
var index = str1.lastIndexOf( "one" );
document.write("lastIndexOf found String :" + index );
</script>
</body>
</html>
這將產生以下結果:
lastIndexOf found String :29 lastIndexOf found String :15
