0

/g. 複数の結果を返したくないようです

次のコードがあります

<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>

byond ローカルアクション 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になるはずです..そして、それを使用して個々の結果を分割する必要があります

誰でも助けてくれますか

4

1 に答える 1

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"]]
于 2013-05-10T05:04:14.100 に答える