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