JS match() 方法?

<html>
<body>

<script type="text/javascript">

var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))

</script>

</body>
</html>
-----------------------------------------------

结果为何是这样!
world
null
null
world!

九州编程
浏览 504回答 2
2回答

慕田峪9158850

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。语法:stringObject.match(searchvalue)stringObject.match(regexp)参数描述:searchvalue 必需。规定要检索的字符串值。regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp&nbsp;构造函数,将其转换为 RegExp 对象。例子:<script type="text/javascript">var str="Hello world!"document.write(str.match("world") + "<br />")document.write(str.match("World") + "<br />")document.write(str.match("worlld") + "<br />")document.write(str.match("world!"))</script>输出:worldnullnullworld!
打开App,查看更多内容
随时随地看视频慕课网APP