JQUERY WORD COUNTER - EXCLUDE STRING IN BRACKETS

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
JQuery 
 1414  View(s)
Ratings:
Rate this:
Hi Expert!

I have an jQuery function for word counting in textarea field. Everything 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;
    }

Thanks in advance! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: jQuery word counter - exclude string in brackets

    Hi Ankita!


    You can exclude words in brackets by making as following

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

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!