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.
Asked
Active
Viewed 341 times
2

Berkay Gunduz
- 79
- 1
- 10
-
1@PeterB I know about that, although I can't use it for containing number. – Berkay Gunduz Aug 04 '18 at 12:37
-
@BerkayGunduz try `.match(/[0-9]+/)` – Martin Zeitler Aug 04 '18 at 23:46
2 Answers
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].*)');

Kevin Bruccoleri
- 354
- 2
- 12
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>

Peter B
- 22,460
- 5
- 32
- 69