2012-10-01 30 views
0

輸入或功能點(始終沿線的東西):的Javascript正則表達式匹配字符串

華盛頓T.趕3碼到MT0

我想要得到的文本「to the MT0

「MT」將與我將在函數調用之前初始化的變量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,但它不是出於某種原因。

我不太確定它的錯在哪裏。你們看到我缺少的東西嗎?謝謝!

回答

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如您所願!你有什麼?

+0

由於某種原因,我得到了-1「華盛頓,T.2秒趕到MT3,1ST DOWN GT(艾倫,克雷格)。」 – ealeon

+0

0k,讓我試試這個字符串... –

+0

奇怪的是,我得到了這個輸入字符串的「H3」。這裏是我的Chrome控制檯:「getEndSpotEugene(」華盛頓,T.趕2碼到MT3,1ST DOWN GT(艾倫,克雷格)。「) /to +(MT | H)?([0-9] {1,2})/ g檢查:38 [「to the MT3」]檢查:40 MT檢查:56 3檢查:57 「H3」。「您在測試什麼瀏覽器? –

相關問題