Assuming I have an Amazon product URL like so
http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C/ref=amb_link_86123711_2?pf_rd_m=ATVP
Inspired by many of the answers here, I found that
(?:[/])([A-Z0-9]{10})(?:[\/|\?|\&|\s|$])
let url="https://www.amazon.com/Why-We-Sleep-Science-Dreams-ebook/dp/B06Y649387/ref=pd_sim_351_4/131-0417603-5732106?_encoding=UTF8&pd_rd_i=B06Y649387&pd_rd_r=5ebbfdd5-a2f6-4ee3-ad13-5036b5e20827&pd_rd_w=LBo2H&pd_rd_wg=OBomS&pf_rd_p=3c412f72-0ba4-4e48-ac1a-8867997981bd&pf_rd_r=TN0WDV3AC7ED4Y7EKNVP&psc=1&refRID=TN0WDV3AC7ED4Y7EKNVP"
url.match("(?:[/])([A-Z0-9]{10})(?:[\/|\?|\&|\s])")
>> Array [ "/B06Y649387/", "B06Y649387" ]
works really well for extracting asin from anywhere in the url. You can try it out here. https://regexr.com/56jm7
edit: Added end-of-string as one of the stopping checks. This is needed when the regex is used in python