如何获取JavaScript中字符串中指定值出现的最后一个索引?
此方法返回最后一次出现的指定值的调用String对象内的索引,从fromIndex或-1开始搜索(如果找不到该值)。
语法
以下是语法-
string.lastIndexOf(searchValue[, fromIndex])
这是参数-
searchValue-一个字符串,代表要搜索的值。
fromIndex-调用字符串中开始搜索的位置。它可以是0到字符串长度之间的任何整数。默认值为0。
示例
您可以尝试运行以下代码以了解如何获取字符串中指定值出现的索引-
<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script>
var str1 = new String( "Learn for free and enjoy! Learn and share!" );
var index = str1.lastIndexOf( "Learn" );
document.write("lastIndexOf找到了字符串:" + index );
document.write("<br />");
var index = str1.lastIndexOf( "and" );
document.write("lastIndexOf找到了字符串:" + index );
</script>
</body>
</html>热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短