2

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-commerce site if it isn't already stated in Open Graph which is already handled.

1

3 Answers 3

3

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

Escape \:

var price = $("div:regex(\\bprice\\b)").text(); 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for this however i am getting this in the console Error: Syntax error, unrecognized expression: unsupported pseudo: regex
regex isn't part of jQuery, you have to use james.padolsey.com/javascript/regex-selector-for-jquery
Great thanks for this getting this now any thoughts? TypeError: 'undefined' is not an object (evaluating 'match[3]')
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/

2 Comments

I have tried using this $('div[id~="price"], div[class~="price"], span[id~="price"], span[class~="price"], p[class~="price"], p[id~="price"]').first().text(); and it works well however seems a little messy any thoughts?
So just use $('[id~="price"], [class~="price"]')
0

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)") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.