views:

786

answers:

3

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

+3  A: 

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");
PhiLho
+2  A: 

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

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

Eric Wendelin
Props to you my friend! That was it!
leeand00
A: 

You don't need to use regular expressions for plain old string matching. You especially don't need to compile and throw away 12 regular expressions every time the function is called.

A saner version:

// Get integer number of named month. 1-indexed for January;
// return 0 if unreadable name.
//
function readMonth(s) {
    var begin= s.toLowerCase().substring(0, 3);
    var ix= MONTHS.indexOf(begin);
    if (ix==-1) return 0;
    return ix/4+1;
}
var MONTHS= 'jan feb mar apr may jun jul aug sep oct nov dec';
bobince
Good idea too, might be faster than the loop, although my first solution is a bit more flexible, if the date format varies.
PhiLho
Yeah, I would usually go for the array instead of the space-separated string hack, but annoyingly Array.indexOf() is not part of standard ECMA-262 or supported by IE.
bobince