JS.match方法 正则表达式

match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。

<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>

输出

world

null

null

world!

<script type="text/javascript">
var str="1 plus 2 equal 3"
document.write(str.match(/d+/g))
</script>

输出

1,2,3(数组)

原文地址:https://www.cnblogs.com/cnundefined/p/match.html