Can a sizzle selector evaluate regular expression?

I need to select links with a specific URL format. Can I use sizzle to evaluate the href link attribute for regular expression?

For example, can I do something like this:

var arrayOfLinks = Sizzle('a[HREF=[0-9]+$]');

create an array of all links on a page whose URL ends with a number?

+3
source share
1 answer

Give it a try. I tried to convert the jQuery regex selector to which Kobe is tied to the Sizzle selector extension. This seems to work, but I have not tested many tests.

Sizzle.selectors.filters.regex = function(elem, i, match){ 
    var matchParams = match[3].split(',', 2);
    var attr = matchParams[0];
    var pattern = matchParams[1];
    var regex = new RegExp(pattern.replace(/^\s+|\s+$/g,''), 'ig');
    return regex.test(elem.getAttribute(attr));
};

In this case, your example will be written as:

var arrayOfLinks = Sizzle('a:regex(href,[0-9]+$)');
+3
source

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


All Articles