Search code examples
htmlcssdomjquery-selectorscheerio

css selector using Cheerio exclusion of double class


I have a rather "weird" scenario (web scraping). I want to select class=g only but not those with class="g g" (double class g). How to do that in jQuery?

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

UPDATE 1:

If you don't think .g .g is valid, do View Source in Google search results ;)


Solution

  • Considering the following scenario where you have two Div tag with above classes

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

    Write following to get only those div which have class"g"

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