2012-10-01 4 views
0

입력 또는 기능의 자리 (항상 라인을 따라 뭔가 일) 텍스트 "to the MT0"자바 스크립트 정규식 일치 문자열

"MT"는 ​​함수 호출 전에 초기화 할 변수 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; 
    }, 

(하나의 간단한 경우) :

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

나는 "Washington, T. MT3, 1ST DOWN GT (Allen, Craig)에서 2 야드 동안 러쉬." – ealeon

+0

0k, 그 문자열을 사용해 보겠습니다 ... –

+0

이상한, 그 입력 문자열에 대해 "H3"을 얻고 있습니다. Chrome의 내 콘솔은 다음과 같습니다. getEndSpotEugene ("Washington, T. MT3, 1ST DOWN GT (Allen, Craig)에 2 야드 러쉬.") /to (MT | H)? ([0-9] {1,2})/g 점검 : 38 [ "MT3"] 점검 : 40 MT 점검 : 56 3 점검 : 57 "H3".' 어떤 브라우저를 테스트하고 있습니까? –