如何获取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 出阁宴祝福语姐姐简短