Css selector using double class cheerio exception

I have a rather strange scenario (web scraper). I want to select class=g , but not those with class="gg" (double class g ). How to do it in jQuery?

If I use $ ('. G'), it will select both .g and .g .g

UPDATE 1:

If you don't think .g .g valid, check out the source in Google search results;)

+4
source share
2 answers

Given the following scenario, in which you have two div tags with the above classes

 <div class="g"></div> <div class="gg"></div> 

Write the following to get only those divs that have class "g"

 alert($("div[class='g']").length); 
+2
source

Even if I don't think gg really (let me check) .. and therefore $(".g:not(.gg)") does not work, you can do something like:

 var myWeirdElements = $('.g').map(function(){return this.className.indexOf('g g') && this ;}); // or, as mentioned on the comment, using Array.filter method 

demo => http://jsfiddle.net/GeAaA/

Edit: about the second comment, you just need to calculate how many g (simple regular expression)

+4
source

All Articles