2

I want to get all elements that class contains a number, how can I do that?
I haven't tried anything because I don't really know how to think about it.

2
  • 1
    @PeterB I know about that, although I can't use it for containing number. Commented Aug 4, 2018 at 12:37
  • @BerkayGunduz try .match(/[0-9]+/) Commented Aug 4, 2018 at 23:46

2 Answers 2

3

This might be related to this question: jQuery selector regular expressions

Using the regex functionality from this page, you should be able to do something like this:

$(':regex(class,.*[0-9].*)');
Sign up to request clarification or add additional context in comments.

Comments

2

This can be done by iterating over all desired elements, keeping only those where the className attribute matches a given regex.

var divs = $('div');
var regex = /\d/;
var result = divs.filter(index => regex.test(divs[index].className));

result.each(index => console.log(result[index]));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="x1" class="a"></div>
<div id="x2" class="a1"></div>
<div id="x3" class="2a"></div>
<div id="x4" class="3"></div>
<div id="x5" class="bc"></div>
<div id="x6" class="x4y5z"></div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.