0

函数中的输入或点(总是沿线):

华盛顿,T. 冲 3 码到 MT0

我想得到文本“ to the MT0

“MT”将与 var 匹配,或者homeacrynmawayacrynm将在函数调用之前初始化。这是我到目前为止所尝试的:

getEndSpotEugene: function(spot)
    {
        var regex = new RegExp("to the +("+homeacrynm+"|"+awayacrynm+")?([0-9]{1,2})","g");
        var matches = spot.match(regex);
        if (matches)
        {
            pos = matches[matches.length-1]
            matches = pos.match(/to the ([A-Z]+)?([0-9]{1,2})/);
            if (!matches[1])
            {
                matches=[pos,"V",50];   
            }
        }
        else
        {
            return -1;  
        }
        var acr = matches[1];
        var yard = matches[2];
        if (acr == homeacrynm) 
            return "H"+yard;
        else
            return "V"+yard;
    },

例如(一个简单的案例):

homeacrynm = "MT"
var giveMe = getEndSpotEugene("Washington, T. rush for 3 yards to the MT11")

giveMe应该是 H11 但它不是出于某种原因。

我也不太确定哪里错了。你们有没有看到我遗漏的东西?谢谢!

4

1 回答 1

0

我做了一些日志并像这样明确声明 homeacrynm 和 awayacrynm :

  var homeacrynm = "MT";
  var awayacrynm = "H";
  var getEndSpotEugene = function(spot)
  {
      var regex = new RegExp("to the +("+homeacrynm+"|"+awayacrynm+")?([0-9]{1,2})","g");
      console.log(regex);
      var matches = spot.match(regex);
      console.log(matches);
      if (matches)
      {
          pos = matches[matches.length-1]
          matches = pos.match(/to the ([A-Z]+)?([0-9]{1,2})/);
          if (!matches[1])
          {
              matches=[pos,"V",50];   
          }
      }
      else
      {
          return -1;  
      }
      var acr = matches[1];
      var yard = matches[2];
      console.log(acr);
      console.log(yard);          
      if (acr == homeacrynm) 
          return "H"+yard;
      else
          return "V"+yard;
  }

奇怪的是,我得到了你所期望的H11!你得到什么?

于 2012-10-01T16:54:59.433 回答