Filter A HTML string for title and meta tags

This week I’ve worked in a little project where I need to get a website title and meta description to autofill the site details on a user’s favourite sites.

To achieve this, my site makes an HTTP request to a URL like “http://google.com” and return to the browser that site’s full HTML code which I filter to get the title and description.

I think it’s an useful script so I decided to share how I filtered the code, so here it is.

//////
// 'response' is the HTTP request result and 'content' 
// is the attribute containing the HTML code
//////

//If you wanna find the title:

results.content.match(/<title[^>]*>([^<]+)<\/title>/)[1]


//If you wanna find the meta tag which contains site description: (jQuery)

$(results.content).filter('meta[name="description"]').attr("content")
$(results.content).filter('meta[name="Description"]').attr("content")


Hope it helps someone! =)