Search code examples
jqueryjquery-selectorsjquery-calculation

jQuery selector that selects class name with only one number following that class name


so I have the following structure:

 <div class="num1">1,2</div>  
 <div class="num11">2,2</div>  

 <div class="num2">5,4</div>  
 <div class="num21">3,2</div>  

 etc...

I want a jQuery selector that will select only num1 and num2, numX where X being only one number. Currently I have a selector:

alert($("div[class^='num']").max());  

which of course returns all of the num's, so I'm wondering if there is some sort of way to handle this easily. Btw, if someone is wondering what the .max() is, it is from jQuery calculation plugin


Solution

  • Here you have it

    $("div[class^='num']").filter(function(){
       return /^num\d$/.test($(this).attr("class"));
    })
    

    It uses a regular expression to select elements with appropiate classnames.

    Hope it helps