如何获取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 2026祝福语简短创意牛
17 孩子军训回家祝福语简短
18 好友上岸成功祝福语简短