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 correctly.
I am using Safari and Chrome on OS X
var price = $("div:regex(\bprice\b)").text();
The purpose is to get the price of a product on an e-merce site if it isn't already stated in Open Graph which is already handled.
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 correctly.
I am using Safari and Chrome on OS X
var price = $("div:regex(\bprice\b)").text();
The purpose is to get the price of a product on an e-merce site if it isn't already stated in Open Graph which is already handled.
Share Improve this question edited Aug 30, 2013 at 8:05 Jason Evans 29.2k15 gold badges94 silver badges156 bronze badges asked Aug 30, 2013 at 8:04 Justin ErswellJustin Erswell 7087 gold badges44 silver badges90 bronze badges 1- Interesting alternate way of doing this : stackoverflow./questions/1163888/… Though may be a perf slug :( But still, interesting. – Jason Evans Commented Aug 30, 2013 at 8:13
3 Answers
Reset to default 3"\b"
is same as "\x08"
.
Escape \
:
var price = $("div:regex(\\bprice\\b)").text();
A simple way is to use contains word selector:
$('div[id~="price"], div[class~="price"]')
See http://api.jquery./attribute-contains-word-selector/
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)")