【发布时间】:2008-11-19 16:51:34
【问题描述】:
monthRegex 正则表达式始终返回 true,即使 dateInput 在我看来类似于“2008 年 12 月 1 日”,它也应该通过我传入的任何键匹配正则表达式。但事实并非如此,它只是返回 true,并将“JAN”检测为月份。
function dateFormat(dateInput) {
var formattedDate = "";
var the_date, month, year;
var monthHash = new Array();
monthHash['JAN']="01";
monthHash['FEB']="02";
monthHash['MAR']="03";
monthHash['APR']="04";
monthHash['MAY']="05";
monthHash['JUN']="06";
monthHash['JUL']="07";
monthHash['AUG']="08";
monthHash['SEP']="09";
monthHash['OCT']="10";
monthHash['NOV']="11";
monthHash['DEC']="12";
// Find which month we are dealing with
var whichKey = null;
for(var key in monthHash) {
var monthRegex = new RegExp(key, "i")
monthRegex.compile();
console.log("monthRegex.compile: " + monthRegex.test(dateInput));
if(monthRegex.test(dateInput))
{
whichKey = key;
break;
}
}
}
谢谢你,
安德鲁·J·李尔
【问题讨论】:
标签: javascript regex