jQuery selector regular expressions

前端 未结 10 2486
傲寒
傲寒 2020-11-21 22:36

I am after documentation on using wildcard or regular expressions (not sure on the exact terminology) with a jQuery selector.

I have looked for this myself but have

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 23:12

    Add a jQuery function,

    (function($){
        $.fn.regex = function(pattern, fn, fn_a){
            var fn = fn || $.fn.text;
            return this.filter(function() {
                return pattern.test(fn.apply($(this), fn_a));
            });
        };
    })(jQuery);
    

    Then,

    $('span').regex(/Sent/)
    

    will select all span elements with text matches /Sent/.

    $('span').regex(/tooltip.year/, $.fn.attr, ['class'])
    

    will select all span elements with their classes match /tooltip.year/.

提交回复
热议问题