Javascript正则表达式匹配字符串

Javascript正则表达式匹配字符串,javascript,regex,match,Javascript,Regex,Match,函数中的输入或点(始终是沿直线的): 华盛顿,T。冲3码到MT0 我想获取文本“到MT0” “MT”将与我将在函数调用之前初始化的varhomeacrynm或awayacrynm匹配。以下是我迄今为止所尝试的: getEndSpotEugene: function(spot) { var regex = new RegExp("to the +("+homeacrynm+"|"+awayacrynm+")?([0-9]{1,2})","g"); var m

函数中的输入或点(始终是沿直线的):

华盛顿,T。冲3码到MT0

我想获取文本“
到MT0

“MT”将与我将在函数调用之前初始化的var
homeacrynm
awayacrynm
匹配。以下是我迄今为止所尝试的:

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;
    },
例如(一个简单的案例):

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


我也不太确定哪里不对。你们看到我遗漏的东西了吗?谢谢大家!

我做了一些日志,并明确声明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!你得到了什么?

出于某种原因,我在“华盛顿,T。冲向MT3 2码,第一个向下GT(Allen,Craig)”上得到了-1。奇怪的是,我的输入字符串得到了“H3”。这是我的Chrome控制台:
getEndSpotEugene(“华盛顿,T.冲向MT3 2码,第一个向下GT(艾伦,克雷格)。”)/to+(MT|H)?([0-9]{1,2})/g checks:38[“to the MT3”]checks:40 MT checks:56 3 checks:57“H3”。
你在哪个浏览器中测试?
  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;
  }