test方法為正規表示式匹配的文字搜尋字串。如果找到匹配,則傳回true; 否則傳回false。
文法
RegExpObject.test( string );
以下是參數的詳細資訊:
傳回值:
範例:
<html> <head> <title>JavaScript RegExp test 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.test(str); document.write("Test 1 - returned value : " + result); re = new RegExp( "pushing", "g" ); var result = re.test(str); document.write("<br />Test 2 - returned value : " + result); </script> </body> </html>
這將產生以下結果:
Test 1 - returned value : true Test 2 - returned value : false