exec方法为正则表达式匹配的文本搜索字符串。如果找到匹配,则返回结果数组; 否则,返回null。
语法
RegExpObject.exec( string );
下面是参数的详细信息:
返回值:
例子:
<html> <head> <title>JavaScript RegExp exec Method</title> </head> <body> <script type="text/javascript"> var str = "Javascript is an interesting scripting language"; var re = new RegExp( "script", "g" ); var result = re.exec(str); document.write("Test 1 - returned value : " + result); re = new RegExp( "pushing", "g" ); var result = re.exec(str); document.write("<br />Test 2 - returned value : " + result); </script> </body> </html>
这将产生以下结果:
Test 1 - returned value : script Test 2 - returned value : null