2017-02-19 3 views
0

mark.jsを正規表現として使用して、文字列の一致を強調表示します。例えば特定のクラスの正規表現の一致数をカウントするためにmarkjsを使用する

//negative assertions 
instance.markRegExp(/not significant/g, {className: "negative"}); 
instance.markRegExp(/not associated/g, {className: "negative"}); 
instance.markRegExp(/no association/g, {className: "negative"}); 
//positive assertions 
instance.markRegExp(/is associated/g, {className: "positive"}); 
instance.markRegExp(/are associated/g, {className: "positive"}); 
instance.markRegExp(/was associated/g, {className: "positive"}); 

私は試合のクラスが発生した回数をカウントできるようにしたいと思います。

documentationは、コールバック機能を示していますが、私はそれはちょっと簡単です、この目的のために

var options = { 
"filter": function(node, term, totalCounter, counter){ 
    if(term === "the" && counter >= 10){ 
     return false; 
    } else { 
     return true; 
    } 
} 
}; 
+0

現在のカウンターを受け取った後、何をしたいですか? – dude

+0

ただvarに保存 – haz

答えて

2

をそれを使用できる場合、私はわかりませんよ。 eachまたはdoneコールバックのいずれかを使用すると、どちらもカウンタを提供できます。 doneコールバックを使用すると、それを自分で数える必要はなく、すべてのマークの数を受け取るので、簡単です。さらに、各マーク上で関数を呼び出す必要はないため、doneコールバックがパフォーマンス向上に役立ちます。

ここでは、コードです:

  1. 非同期で動作しますmark.js:ここ

    var instance = new Mark(".context"), 
     
        negativeCounter = 0, 
     
        positiveCounter = 0; 
     
    
     
    //negative assertions 
     
    instance.markRegExp(/not significant/g, { 
     
        className: "negative", 
     
        done: function(counter) { 
     
        negativeCounter += counter; 
     
        } 
     
    }); 
     
    instance.markRegExp(/not associated/g, { 
     
        className: "negative", 
     
        done: function(counter) { 
     
        negativeCounter += counter; 
     
        } 
     
    }); 
     
    instance.markRegExp(/no association/g, { 
     
        className: "negative", 
     
        done: function(counter) { 
     
        negativeCounter += counter; 
     
        } 
     
    }); 
     
    //positive assertions 
     
    instance.markRegExp(/is associated/g, { 
     
        className: "positive", 
     
        done: function(counter) { 
     
        positiveCounter += counter; 
     
        } 
     
    }); 
     
    instance.markRegExp(/are associated/g, { 
     
        className: "positive", 
     
        done: function(counter) { 
     
        positiveCounter += counter; 
     
        } 
     
    }); 
     
    instance.markRegExp(/was associated/g, { 
     
        className: "positive", 
     
        done: function(counter) { 
     
        positiveCounter += counter; 
     
        } 
     
    }); 
     
    
     
    document.write("Positive counter: " + positiveCounter + ", Negative counter: " + negativeCounter);
    <script src="https://cdn.jsdelivr.net/mark.js/8.8.3/mark.min.js"></script> 
     
    <div class="context"> 
     
        not significant not significant not associated no association is associated are associated was associated 
     
    </div>

    は、いくつかのメモです。理論的には、doneコールバックに.mark()コールをネストする必要があります。ただし、iframeオプションを有効にしていないため、このオプションは機能します。しかし、これを行うと安全です

  2. .mark()呼び出しを減らしてコードを簡単にするには、1つのRegExpを負の数に、1つを正の一致として作成する必要があります。 RegExpグループを使用します。
+0

ありがとう、それは動作します。私はあなたの答えを見いだすまで、このタイプのものを使うつもりでした:positive_count = positive_count + source.match(/は関連/ g).length – haz

関連する問題