如何获取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 给男生的简短祝福语