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