如何获取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 婆婆高寿祝福语大全简短