时间:2021-07-01 10:21:17 帮助过:38人阅读
我们先来看一下match方法的基本语法
- string.match( param )
param表示正则表达式对象。
注意:如果正则表达式不包含g修饰符(执行全局搜索),则match()方法将仅返回字符串中的第一个匹配项。
如果未找到匹配项,则此方法返回null。
下面我们来看具体的示例
代码如下
- <!DOCTYPE html>
- <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
本篇文章到这里就全部结束了,更多精彩内容大家可以关注Gxl网的相关栏目教程!!!
以上就是match方法怎么使用的详细内容,更多请关注Gxl网其它相关文章!