這篇文章主要介紹了JavaScript中的lastIndexOf()方法使用詳解,是JS入門學習中的基礎知識,需要的朋友可以參考下
此方法調用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
|
新聞熱點
疑難解答
圖片精選