Javascript Hashmap key compiled into a regular expression

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

Answers


Remove the "monthRegex.compile();" line and it works.

This is because monthRegex.compile(); complies "" as a regex and therefore everything matches it.


First remark: don't use Array as associative array! Use Object instead. Or use the Array in the reverse way.

Second remark: why do you use regexes for such simple search? Use indexOf instead:

function dateFormat(dateInput) 
{
  var formattedDate = "";

  var the_date, month, year;

  var months = new Array("", 
      "jan", "feb", "mar", 
      "apr", "may", "jun", 
      "jul", "aug", "sep", 
      "oct", "nov", "dec"
  );

  // Find which month we are dealing with
  for (var i = 1; i < months.length; i++) 
  {
    if (dateInput.toLowerCase().indexOf(months[i]) > -1)
    {
      var whichMonth = months[i];
      break;
    }
  }
  if (whichMonth != undefined)
    alert("Found: "  + whichMonth);
}
dateFormat("10 Jun 2008");

If you really want to use regexes, to remain in the topic, here is another way:

function dateFormat(dateInput) 
{
  var formattedDate = "";

  var the_date, month, year;

  var months = /(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i;

  // Find which month we are dealing with
  var matches = dateInput.match(months);
  if (matches != null)
    alert("Found: "  + matches[1]);
}
dateFormat("December, 10 2008");

Need Your Help

Choosing a strategy for BI module

reporting solr olap business-intelligence oltp

The company I work for produces a content management system (CMS) with different various add-ons for publishing, e-commerce, online printing, etc. We are now in process of adding "reporting module"...

svn relocate error — invalid relocation destination

svn

Our svn repo's location was changed to a different url, and in addition the relative paths of trunk, branches, and tags were moved a little. Now when I try to svn relocate, I get this message.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.