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