0

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

        }
    });
};
mm1975
  • 1,583
  • 4
  • 30
  • 51
  • http://stackoverflow.com/questions/2196641/how-do-i-make-jquery-contains-case-insensitive-including-jquery-1-8 – Kristoffer Jälén Sep 02 '14 at 19:51
  • Keep in mind you only search inside `` elements, whereas text might as well be inside a `
    `, `

    ` (such as your question) or something else. So you will be missing a lot of text.

    – Daniël Knippers Sep 02 '14 at 19:55
  • Your right, but I search additional for p, h1, h2, ... Thank you for your advice – mm1975 Sep 02 '14 at 19:59

3 Answers3

1

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);
        }
    });
Mindastic
  • 4,023
  • 3
  • 19
  • 20
0

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

0

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

Mindastic
  • 4,023
  • 3
  • 19
  • 20