2013-05-10 1 views
0

특정 결과 (1보다 클 수 있음)를 찾고/g와 관련하여 문제를 처리하려고합니다. 나던 복수의 결과 regedx는 두 개 이상의 결과가 반환되는 gloabl 척도로 작업합니다.

을 반환 할 것 같다 나는 내가은 성공적 지역 활동의 각 인스턴스를 얻기 위해 노력하고

<div id="test"> 
<p> <!--{Byond:cta|localAction:contact|Contact Us}--><\/p> 
<p> &nbsp;<\/p> <p> <sub><strong>Important Information:<\/strong>&nbsp;Offer 
only available for new home loans. *Comparison Rate is calculated on a loan amount of 
$150,000 over a term of 25 years. **Minimum redraw of $500. ^Limits apply for fixed rate 
home loans.<\/sub><\/p> <!--{Byond:cta|localAction:product:45|Product}--> 
</div> 

다음 코드는 AMD의 다음

내가

을 사용하고있는 문자열을 분할해야
var introduction = $("#test").html(); 
var initExpr = /<!--{Byond\:cta\|localAction[^}]+}-->/gm; 
var initResult = initExpr.exec(introduction); 


    // this result is always 1... WHY? 
    var length = initResult.length; 

    //based on the length split the results up 

for (var i = 0; i < length; i++) { 
var expr = /(?:<!--{Byond\:cta\|)(.*)\|(.*)(?:}-->)/i; 
var result = expr.exec(introduction); 
console.log(result[0], "String"); 
console.log(result[1], "Local Action"); 
console.log(result[2], "Button Name"); 


} 

첫 번째 결과 길이가 1 ... 2가되어야하고 개별 결과를 나눠서 계산해야합니다.

누구든지 도와 줄 수 있습니까?

+0

당신은 중괄호를 이스케이프해야합니다

당신은 당신이 가진 찾고있는 결과를 가질 수있다. ':'는 탈출 할 필요가 없다. –

답변

1

모든 일치 항목을 얻으려면 exec() 메서드를 while 루프에 넣어야합니다. 당신은 얻을

<script type="text/javascript"> 
    var subject = document.getElementById('test').innerHTML; 
    var pattern = /<!--\{Byond:cta\|([^|]+)\|([^|}]+)\}-->/g; 
    var result = new Array(); 
    while((match = pattern.exec(subject)) != null) { 
     result.push(match); 
    } 
</script> 

: 그들은 문자 클래스 외부에있는 경우

[["<!--{Byond:cta|localAction:contact|Contact Us}-->", 
    "localAction:contact", 
    "Contact Us"], 
["<!--{Byond:cta|localAction:product:45|Product}-->", 
    "localAction:product:45", 
    "Product"]] 
+0

완벽한 - 감사합니다. – Dan

관련 문제