test 方法
返回一个 Boolean 值,它指出在被查找的字符串中是否存在模式。rgexp.test(str) 参数
rgexp必选项。包含正则表达式模式或可用标志的正则表达式对象。str必选项。要在其上测试查找的字符串。说明
test 方法检查在字符串中是否存在一个模式,如果存在则返回 true,否则就返回 false。 全局 RegExp 对象的属性不由 test 方法来修改。示例
下面的例子举例说明了 test 方法的用法:function TestDemo(re, s){
   var s1;                         // 声明变量。
   // 检查字符串是否存在正则表达式。
   if (re.test(s))                 // 测试是否存在。
      s1 = " contains ";           // s 包含模式。
   else
      s1 = " does not contain ";   // s 不包含模式。
   return("'" + s + "'" + s1 + "'"+ re.source + "'"); // 返回字符串。
}

解决方案 »

  1.   

    楼上的谢谢你的回帖,我知道test怎么用。但不知道多次调用返回不同结果why
      

  2.   

    <script type="text/javascript">
    <!--
    var re = /^\d+(?:\.\d)?$/ig;alert(re.lastIndex );
    alert(re.test('112.3'));
    alert(re.lastIndex );
    alert(re.test('112.3'));//-->
    </script>
    lastIndex Property
    This property is an integer that specifies the index at which to start the next match, but is only set if the regular expression uses the 'g' flag to specify a global search.Syntax: object.lastIndex
      

  3.   

    test会修改lastIndex的值
    第一次匹配后lastIndex在字符串末尾,就是字符串的长度了第二次从末尾这个位置这个匹配肯定找到到了
    <script type="text/javascript">
    <!--
    var re = /^\d+(?:\.\d)?$/ig;   alert(re.test('112.3'));
    re.lastIndex=0;//加这句试试
    alert(re.test('112.3'));//-->
    </script>