mm1975
mm1975

Reputation: 1655

Find (case insensitive) Words on an external Website and count them

I use this snippet to find and count words on an external Website. How can I search case insensitive? For e.g.: Test, test or TEST - everything counts? I use the current jQuery-Version. Thank you four your tips.

    function keyWordSearch() {
    var kWord = jQuery('#keywords').val();
    var webSite = jQuery('#urls').val();
    var spanSelector = "span:contains(" + kWord + ")";

    $.ajax({
        url : webSite,
        success : function(data) {
            //check in span//
            spanKeyword = 0;
            var message = $('<div/>').append(data).find(spanSelector).each(function() {
                spanKeyword += parseInt($(this).text().split(kWord).length - 1);
            });
            alert('span ' + spanKeyword);

        }
    });
};

Upvotes: 0

Views: 50

Answers (3)

Mindastic
Mindastic

Reputation: 4131

This is my new answer:

$.ajax({
        url : webSite,
        success : function(data) {
            //check in span//
            var spanKeyword = 0;
            var regExp = new RegExp("(^|\\W)" + kWord + "(\\W|$)", "gi");
            jQuery(data).find("span").each(function(){
              var data = jQuery(this).text();
              spanKeyword += data.match(regExp).length;
            });
            alert('span ' + spanKeyword);
        }
    });

Upvotes: 1

Mindastic
Mindastic

Reputation: 4131

Why don't you try using a regular expression?

You could probably do:

$.ajax({
    url : webSite,
    success : function(data) {
        //check in span//
        var regExp = new RegExp("(^|\\W)" + kWord + "(\\W|$)", "gi");

        spanKeyword = data.match(regExp).length;

        alert('span ' + spanKeyword);

    }
});

Hope this helps.

Regards,

Marcelo

Upvotes: 0

user3991493
user3991493

Reputation:

If case is immaterial why arent you using .toLowerCase() on your text?

spanKeyword += parseInt($(this).text().toLowerCase().split(kWord).length - 1);

or to make your keyword case insentive

var kWord = jQuery('#keywords').val().toLowerCase();

considering that kWord is string

Upvotes: 0

Related Questions