The monthRegex regular expression always returns true, even if dateInput is something like ‘December 1, 2008’ by my thoughts it should match a regular expression by whichever key I pass into it. But that isn’t what happens, it just returns true, and detects ‘JAN’ as the month.
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; } } }
Thank you,
Andrew J. Leer
Remove the ‘monthRegex.compile();’ line and it works.
This is because monthRegex.compile(); complies ” as a regex and therefore everything matches it.