无涯教程-Javascript - match()函数

111 阅读1分钟

此方法用于在将字符串与正则表达式进行匹配时检索匹配项。

match() - 语法

string.match( param )

param  -  正则表达式对象。

match() - 返回值

  • 如果正则表达式不包括g标志,它将返回与regexp.exec(String)相同的输出。

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

match() - 示例

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

运行上面代码输出

Chapter 3.4.5.1,Chapter 3.4.5.1,.1

参考链接

www.learnfk.com/javascript/…