遇到不懂的问题,多查查api文档。(话说现在论坛上传不了附件~~郁闷中~~)
test
Executes the search for a match between a regular expression and a specified string. Returns true or false.
方法源 RegExp
实现版本 Navigator 4.0, Netscape Server 3.0
语法
regexp.test(str)
参数
regexp The name of the regular expression. It can be a variable name or a literal.
str (Optional) The string against which to match the regular expression. If omitted, the value of RegExp.input is used.
描述
When you want to know whether a pattern is found in a string use the test method (similar to the String.search method); for more information (but slower execution) use the exec method (similar to the String.match method).
示例
The following example prints a message which depends on the success of the test:
function testinput(re, str){
if (re.test(str))
midstring = " contains ";
else
midstring = " does not contain ";
document.write (str + midstring + re.source);
}