Skip to content Skip to sidebar Skip to footer

Jquery Selector With Regex

I am looking to scan a page and look for any html elements that have a class or id that contains the word price. My thought was to use regex here but I cannot get it to fire correc

Solution 1:

"\b" is same as "\x08".

Escape \:

var price = $("div:regex(\\bprice\\b)").text();

Solution 2:

A simple way is to use contains word selector:

$('div[id~="price"], div[class~="price"]')

See http://api.jquery.com/attribute-contains-word-selector/

Solution 3:

You can do that by adding new pseuo selector:

jQuery.expr[':'].regex = function(elem, index, match) {
    var regex = new RegExp(match[3]),
        $elem = $(elem);
    return regex.test($elem.attr('class')) || regex.test($elem.attr('id')); 
};

and you can use this selector using:

$("div:regex(\\bprice\\b)")

Post a Comment for "Jquery Selector With Regex"