scrape ASIN from Amazon URL using javascript


Assuming I have an Amazon product URL like so

How could I scrape just the ASIN using javascript? Thanks!

Amazon's detail pages can have several forms, so to be thorough you should check for them all. These are all equivalent:

They always look like either this or this:<SEO STRING>/dp/<VIEW>/ASIN<VIEW>/ASIN

This should do it:

var url = "";
var regex = RegExp("[\\w-]+/)?(dp|gp/product)/(\\w+/)?(\\w{10})");
m = url.match(regex);
if (m) {
    alert("ASIN=" + m[4]);