javascript – Find elements by attribute regex

Question:

I get for processing all elements that have the data-lang attribute

$("[data-lang]").each(function(index, element) {

  console.log(element)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="shop-input" type="text" maxlength="100" data-lang-placeholder="Filter">
<div id="shop-refresh" class="slim-content-btn noselect" data-lang="shop_refresh"></div>

But I also have data-lang-tittle attributes and the like. How to capture them too, tried $("[^data-lang]") does not exit

Answer:

Personally, in my opinion, the solution below is worse than if, nevertheless, we add classes to the elements, but here the author of the question has to decide:

$('*').filter(function() {
    for(var prop in $(this).data()) {
      console.log(prop);
        if(prop.indexOf('lang') !== -1) {
            return true;
        }
    }

    return false;
}).css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-lang="hello">One</div>
<div data-lang-new="hello">Two</div>
<div data-not-this="hello">Three</div>

PS my comment to the question (link to JSFiddle) contains an error, who noticed – that fellow =)

Scroll to Top