- 1、数组对象.search(“查找内容”);
-查找到返回第一个下标,没有找到返回-1
<html>
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.search("wold") + "<br />")
document.write(str.search("o w") + "<br />")
document.write(str.search("World") + "<br />")
document.write(str.search("world") + "<br />")
</script>
</body>
</html>
结果:
-1
4
-1
6
- 2、数组对象. match(“查找内容”);
-如果存在则返回该字符串,否则返回NULL
<html>
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("wold") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!")+ "<br />")
document.write(str.match("ello") + "<br />")
</script>
</body>
</html>
结果:
null
null
null
world!
ello
- 总结:查找的·是连续的子字符串
- 3、 数组对象.indexOf(“检索的内容”)
—如果要检索的字符串值没有出现,则该方法返回 -1。找到则返回 searchvalue 的第一次出现的位置。
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("wld") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world") + "<br />")
document.write(str.indexOf("orld"))
</script>
</body>
</html>
结果:
-1
-1
6
7
*总结:他检索的时候必须是连续的查找就像第一个他就表示没有查出来,也分大小写