I have a simple list of elements looking like this:
<ul class="items-list">
<li class="item item-1"></li>
<li class="item item-2"></li>
<li class="item item-3"></li>
<li class="item item-4"></li>
</ul>
I select the list by
items = document.getElementsByClassName('items-list')[0]
Finally inside a for..in
loop I want to extract class name that is 'item-*'
.
As I want to make it without jQuery or other libraries, I wonder how I can do it in the most elegant way, something like
if (item.classList.contains('item-.*'))
do_something()
Please advise.
You can check if
some
(ES5) class matches your regExp:Or, in ES6, using arrow functions, you can simplify that to
But of course, if you want maximum performance, use a loop:
http://jsfiddle.net/tcs6b/ perhaps?