📜 ⬆️ ⬇️

RegEx Selector for jQuery

Everyone has long known that jQuery is easily extensible. Today we will add a new selector -: regex.



Extending jQuery


')
Add the following code to the pages you want to use: regex-selector.

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)); } 


Usage: regex



// Select all divs with classes containing numbers:

 $('div:regex(class,[0-9])'); 


// Select all SCRIPT tags with the SRC parameter containing the jQuery string:

 $('script:regex(src,jQuery)'); 


// Selects all elements with the width parameter between 100 and 300:

 $(':regex(css:width, ^[1-3]\\d{2}px$)'); 


Attention! Instead of a single backslash, you need to use a double backslash, for example, \\ d, \\ S, \\ /, \\.

Source: https://habr.com/ru/post/165197/


All Articles