【问题标题】:Javascript Hashmap key compiled into a regular expressionJavascript Hashmap键编译成正则表达式
【发布时间】: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


    【解决方案1】:

    第一点:不要使用数组作为关联数组!请改用对象。或者以相反的方式使用数组。

    第二个评论:你为什么使用正则表达式进行如此简单的搜索?改用 indexOf:

    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");
    

    如果你真的想使用正则表达式,留在主题中,这里有另一种方法:

    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");
    

    【讨论】:

      【解决方案2】:

      去掉“monthRegex.compile();”行,它的工作原理。

      这是因为monthRegex.compile();符合 "" 作为正则表达式,因此一切都匹配它。

      【讨论】:

      • 支持你,我的朋友!就是这样!
      【解决方案3】:

      对于普通的旧字符串匹配,您不需要使用正则表达式。尤其不需要每次调用函数时编译和丢弃 12 个正则表达式。

      一个更理智的版本:

      // 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';
      

      【讨论】:

      • 好主意,可能比循环更快,虽然我的第一个解决方案更灵活一点,如果日期格式不同。
      • 是的,我通常会选择数组而不是空格分隔的字符串,但令人讨厌的是 Array.indexOf() 不是标准 ECMA-262 的一部分,也不支持 IE。
      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多