A
A
Abc Edc2015-11-12 11:40:21
JavaScript
Abc Edc, 2015-11-12 11:40:21

how to do pattern search in jquery

I'm building a tree in jquery, I've learned how to add a child, but it's still a problem with siblings. For brothers, you need to somehow look for it in the pattern. For example, at best

$('body').find('[data-for-tree="some[closure][0][closure][\\d]]"')

// Where \d I meant any number . Moreover, it would be nice if a strict match would be returned,
that is, the regexp itself would be
/some/[closure/]/[0/]/[closure/]/[\\d/]/
, that is, everything after /[\\ d/]/ did not search
Found a solution :regexp
jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^s+|s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}


I call:
$(' :regex(data-for-tree,/some\[closure\]\[\d\]/)')

without result

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2015-11-12
@gleber1

<div data-for-tree="some[closure][1]">child 1</div>
<div data-for-tree="some[closure][2]">child 2</div>

$(':regex(data-for-tree,some\\[closure\\]\\[\\d\\])')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question