求年月的正则表达式yyyy-MM
javascript脚本验证正则表达式
我有一个年月日的正则表达式想换成判断年月的正则表达式
年月日判断的如下 这个好使
case "yyyy-mm-dd " :
//m = op.match(new RegExp( "^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$ "));
m = op.match(new RegExp( "^\\s*((\\d{4})|(\\d{2}))([-])(\\d{2})\\4(\\d{2})\\s*$ "));
if(m == null ) return false;
day = m[6];
month = m[5];
year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
newdate = year+ '/ '+month+ '/ '+day;
date = new Date(newdate);
return (typeof(date) == "object " && year == date.getFullYear() && (month-1) == date.getMonth() && day == date.getDate());
break;
------解决方案-------------------- function checkDate(objdate)
{
var re=/\d{4}\/\d{2}\//;
var re1 = /\d{4}\d{2}/;
if(re.test(objdate) || re1.test(objdate))
{
return true;
}
return false;
}
------解决方案--------------------String.prototype.isDate = function()
{
var r = this.match(/^(\d{1,4})(-)(\d{1,2})$/);
if(r==null)return false; var d = new Date(r[1], r[3]-1, '1 ');
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()== '1 ');
}
alert( "2002-1 ".isDate());
alert( "2002-16 ".isDate());