同一正则测试两次出现的怪问题?
<script language=javascript>
str1= "2007-01-01 ";
str1= "2008-01-01 ";
var my_reg=/^\d{4}-\d{1,2}-\d{1,2}$/g;
alert(my_reg.test( "2007-01-01 ")); //true
alert(my_reg.test( "2008-01-01 ")); //false
execScript( 'n = DateDiff( "d ", str1, str2) ', 'vbscript ');
alert( "两日期间相隔 "+ n + " 天 ");
</script>
问题:
1、为何第一次为true,第二次为false?
2、execScript( 'n = DateDiff( "d ", str1, str2) ', 'vbscript '); 这一句代码在这里运行能得出正确的结果,要是写到一个函数里调用就得不到正确的结果,为何?
请高人指点
------解决方案-------------------- <script type= "text/javascript ">
str1= "2008-01-01 ";
str2= "2007-01-01 ";
var my_reg=/^\d{4}\-\d{1,2}\-\d{1,2}$/;
alert(my_reg.test( "2007-01-01 ")); //true
alert(my_reg.test( "2008-01-01 ")); //true
function DateDiff(a, b) {
var $1 = eval( "new Date( " + a.split( "- ") + ") ");
var $2 = eval( "new Date( " + b.split( "- ") + ") ");
return ($1 - $2) / (24000 * 3600);
}
alert( "两日期间相隔 "+ DateDiff(str1, str2) + " 天 ");
var d;
execScript( "d = DateDiff( 'd ', ' " + str2 + " ', ' " + str1 + " ') ", "vbs ")
alert( "两日期间相隔 "+ d + " 天 ");
</script>