Jiří Valoušek
Jiří Valoušek

Reputation: 691

jQuery word counter - exclude string in brackets

i have an jQuery function for word counting in textarea field. Everithing works fine, but i need exclude words enclosed in triple brackets - [[[for example this string]]] from counter. How do I modify the regular expression to make it work?


    function word_count(field) {
        var number = 0;
        var matches = $(field).val().match(/\b/g);

        if (matches) {
            number = matches.length / 2;
        }
        wordCounts[field] = number;
        var wordCounter = 0;
        $.each(wordCounts, function(k, v) {
            wordCounter += v;

        });
        return wordCounter;
    }

Upvotes: 0

Views: 583

Answers (2)

Ivan Drinchev
Ivan Drinchev

Reputation: 19591

You can exclude words in brackets by making

var matches = $(field).val().replace(/\[\[\[.*\]\]\]/g, '').match(/\b/g);

This way you'll remove any string in the brackets and count the other one.

Upvotes: 1

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

You can remove enclosed text before further processing. Use this:

text = text.replace(/\[\[\[[^\]]*\]\]\]/g, "");

Upvotes: 1

Related Questions