JavaScript中String.match()方法的使用详解

  作者:bea

此方法用于当匹配针对正则表达式的字符串来检索匹配。 语法 string.match( param ) 下面是参数的详细信息: param : 正则表达式对象 返回值: 如果正则表达式不包括g标志,返回的结果相同于regexp.exec(string) 如果正则表达式包含g标志,则该方法返回一个包含所有匹配的数组 例子: <html><head><title>JavaScript String match() M

 此方法用于当匹配针对正则表达式的字符串来检索匹配。
语法




string.match( param )



下面是参数的详细信息:



  •     param : 正则表达式对象


返回值:



  •     如果正则表达式不包括g标志,返回的结果相同于regexp.exec(string)

  •     如果正则表达式包含g标志,则该方法返回一个包含所有匹配的数组


例子:




<html>
<head>
<title>JavaScript String match() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "For more information, see Chapter 3.4.5.1";
var re = /(chapter d+(.d)*)/i;
var found = str.match( re );

document.write(found );

</script>
</body>
</html>






有用  |  无用

猜你喜欢