I'm trying to build the perfect amazon link regex for use in javascript. This is what I have so far:
var reg = /https?:\/\/(www|smile)\.amazon\\/(?:(?:[\w-]+\/)?(?:dp|gp\/product)\/(\w{10})\/)?/;
I want this to match all of the following urls in their entirety:
/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
/?ie=UTF8&camp=1789&creative=9325&linkCode=ur2&ref_type=generic&refcust=5FNWKEJKP63HFBSY6JGLXL4XIQ&tag=menasheh02-20&linkId=HR76ZTGJKWO5ED2N
.html?ie=UTF8&location=https%3A%2F%2Fwww.amazon%2Fgp%2Fsubscribe-and-save%2Fmanager%2Fviewsubscriptions%3Fie%3DUTF8%26ref_%3Dya%255FT15%255F33&tag=menasheh02-20&linkCode=ur2&camp=1789&creative=390957
;refcust=7EATHY4IXOFTTEMLIHVC3YL6DI&ref_type=generic
/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
;rh=n%3A165793011%2Cn%3A!2334111011%2Cn%3A!2334173011%2Cn%3A15539865011%2Cp_n_age_range%3A165936011%2Cp_72%3A1248963011&bbn=15539865011&ie=UTF8&qid=1476851901&rnid=1248961011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
;pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
;pf_rd_p=8e268714-ad3d-444b-b0df-d51d8825fb02&pf_rd_s=events-center-c-7&pf_rd_t=701&pf_rd_i=HTL_desktop&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB
/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
;heroAsin=B0005ZH4QI&source=grid_db_13285418011&pf_rd_p=782d30de-8b22-4b3d-9009-0f7a0cb995d3&pf_rd_s=merchandised-search-3&pf_rd_t=Landing&pf_rd_i=13285418011&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=PPNJHXVZRMM4XP9KXGGG
;pf_rd_p=8e268714-ad3d-444b-b0df-d51d8825fb02&pf_rd_s=events-center-c-7&pf_rd_t=701&pf_rd_i=HTL_desktop&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB
;rh=n%3A165793011%2Cn%3A!2334111011%2Cn%3A!2334173011%2Cn%3A15539865011%2Cp_n_age_range%3A165936011%2Cp_72%3A1248963011&bbn=15539865011&ie=UTF8&qid=1476851901&rnid=1248961011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
And none of these:
;site=&source=hp&q=bad+regex&oq=bad+regex&gs_l=hp.3..0j0i22i30k1l9.724.2089.0.2265.10.9.0.0.0.0.269.1091.0j4j2.6.0....0...1c.1.64.hp..4.5.821.0..0i20k1j0i131k1j0i10k1.k62wRudUpsw
;otherrandomstuff=2
Right now, it's not matching either of the bad ones - that part is relatively simple. (It's also not matching the url with gp/redirect.html?
in it.) The tricky part is getting the match to return each useful part of the url separately, especially considering the if/elses and #.
# Working #
match[1]
should equal either "www" or "smile."
match[2]
should equal the ASIN, or be blank if the url doesn't have /dp/%ASIN%
, %SEO-string%/dp/%ASIN%
, or /gp/product/%ASIN%
# Not Working #
match[3]
should equal the rest of the url after the , or after the product if ASIN is set, but NOT including a # at the end
match[4]
should equal from the start of match[3] to tag=
, if that is present.
match[5]
should equal the tag parameter, if it exists
match[6]
should equal the rest of the url between the tag parameter (if it exists; otherwise blank) and the # (if it exists, otherwise to end)
match[7]
should equal the # at the end and anything after it, or blank if there is not one
I'm just getting into the more plicated regexes, and getting stuck on things like not going all the way to the end of the line if there's a #, etc;.
Can anybody more experienced help? Thanks.
I'm trying to build the perfect amazon link regex for use in javascript. This is what I have so far:
var reg = /https?:\/\/(www|smile)\.amazon\.\/(?:(?:[\w-]+\/)?(?:dp|gp\/product)\/(\w{10})\/)?/;
I want this to match all of the following urls in their entirety:
http://smile.amazon./dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
http://www.amazon./gp/family/signup/info/?ie=UTF8&camp=1789&creative=9325&linkCode=ur2&ref_type=generic&refcust=5FNWKEJKP63HFBSY6JGLXL4XIQ&tag=menasheh02-20&linkId=HR76ZTGJKWO5ED2N
http://www.amazon./gp/redirect.html?ie=UTF8&location=https%3A%2F%2Fwww.amazon.%2Fgp%2Fsubscribe-and-save%2Fmanager%2Fviewsubscriptions%3Fie%3DUTF8%26ref_%3Dya%255FT15%255F33&tag=menasheh02-20&linkCode=ur2&camp=1789&creative=390957
http://www.amazon./gp/student/signup/info?ie=UTF8&refcust=7EATHY4IXOFTTEMLIHVC3YL6DI&ref_type=generic
http://www.amazon./gp/video/primesignup?tag=menasheh02-20
https://smile.amazon./dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
https://smile.amazon./s/ref=s9_acss_gb_cg_HTLLPCGB_3d1?fst=as%3Aoff&rh=n%3A165793011%2Cn%3A!2334111011%2Cn%3A!2334173011%2Cn%3A15539865011%2Cp_n_age_range%3A165936011%2Cp_72%3A1248963011&bbn=15539865011&ie=UTF8&qid=1476851901&rnid=1248961011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
https://www.amazon./b/ref=s9_acss_gb_cg_HTLLPCGB_11a1?node=13521759011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
https://www.amazon./Doctor-Vortex-Manipulator-Sonic-Screwdriver/dp/B001PR1ZII/ref=gbph_tit_e-7_fb02_fc8a0d34?smid=AOUT97QIB451U&pf_rd_p=8e268714-ad3d-444b-b0df-d51d8825fb02&pf_rd_s=events-center-c-7&pf_rd_t=701&pf_rd_i=HTL_desktop&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB
https://www.amazon./dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
https://www.amazon./gp/coupon/skippy-baking-sale/A2UI00T2I5JAV3?ie=UTF8&heroAsin=B0005ZH4QI&source=grid_db_13285418011&pf_rd_p=782d30de-8b22-4b3d-9009-0f7a0cb995d3&pf_rd_s=merchandised-search-3&pf_rd_t=Landing&pf_rd_i=13285418011&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=PPNJHXVZRMM4XP9KXGGG
https://www.amazon./Monster-High-School-Playset/dp/B006O6F932/ref=gbph_tit_e-7_fb02_85d3d028?smid=A3CXJV2JYTL237&pf_rd_p=8e268714-ad3d-444b-b0df-d51d8825fb02&pf_rd_s=events-center-c-7&pf_rd_t=701&pf_rd_i=HTL_desktop&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB
https://www.amazon./s/ref=s9_acss_gb_cg_HTLLPCGB_3d1?fst=as%3Aoff&rh=n%3A165793011%2Cn%3A!2334111011%2Cn%3A!2334173011%2Cn%3A15539865011%2Cp_n_age_range%3A165936011%2Cp_72%3A1248963011&bbn=15539865011&ie=UTF8&qid=1476851901&rnid=1248961011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=events-center-c-4&pf_rd_r=8MKN8SY6C5ZP4NC1C0RB&pf_rd_t=701&pf_rd_p=e4acec8d-70de-466a-be44-05291b40a5d4&pf_rd_i=HTL_desktop
And none of these:
https://www.google./search?safe=active&site=&source=hp&q=bad+regex&oq=bad+regex&gs_l=hp.3..0j0i22i30k1l9.724.2089.0.2265.10.9.0.0.0.0.269.1091.0j4j2.6.0....0...1c.1.64.hp..4.5.821.0..0i20k1j0i131k1j0i10k1.k62wRudUpsw
https://sellercentral.amazon./B53C945A8D?randomstuff=34341&otherrandomstuff=2
Right now, it's not matching either of the bad ones - that part is relatively simple. (It's also not matching the url with gp/redirect.html?
in it.) The tricky part is getting the match to return each useful part of the url separately, especially considering the if/elses and #.
# Working #
match[1]
should equal either "www" or "smile."
match[2]
should equal the ASIN, or be blank if the url doesn't have /dp/%ASIN%
, %SEO-string%/dp/%ASIN%
, or /gp/product/%ASIN%
# Not Working #
match[3]
should equal the rest of the url after the ., or after the product if ASIN is set, but NOT including a # at the end
match[4]
should equal from the start of match[3] to tag=
, if that is present.
match[5]
should equal the tag parameter, if it exists
match[6]
should equal the rest of the url between the tag parameter (if it exists; otherwise blank) and the # (if it exists, otherwise to end)
match[7]
should equal the # at the end and anything after it, or blank if there is not one
I'm just getting into the more plicated regexes, and getting stuck on things like not going all the way to the end of the line if there's a #, etc;.
Can anybody more experienced help? Thanks.
Share Improve this question edited Nov 16, 2016 at 5:14 Menasheh asked Nov 16, 2016 at 4:58 MenashehMenasheh 3,7085 gold badges34 silver badges52 bronze badges 10-
Instead of
RegExp
constructor, use regex literal. – Tushar Commented Nov 16, 2016 at 5:02 - @Tushar for use with the match function? How do you mean? – Menasheh Commented Nov 16, 2016 at 5:06
-
1
var regex = /https?:\/\/(www|smile)\.amazon\.\/(?:(?:[\w-]+\/)?(?:dp|gp\/product)\/(\w{10})\/)?([\w\/=-]+)?([\w\/?=-]+)?(\?.+)(#[\w]+)?/;
then # Not Working In JS # problems will be solved. InRegExp
constructor as string is passed the slashes need to be escaped. – Tushar Commented Nov 16, 2016 at 5:07 -
1
For the record, using the
URL
API to subdivide the URL and process ponents independently might be something to consider. Create an anchor tag with the URL (don't even need to put it in DOM) if you don't have one, then you can accessanchor.hostname
,anchor.pathname
,anchor.search
, etc., so you're not trying to do all the work in a single unreadable regex. – ShadowRanger Commented Nov 16, 2016 at 5:07 - @Tushar Thanks, now it works as well in js as in regexr. I probably lost some better regex to toying with that... oh well. Anyway, question updated. – Menasheh Commented Nov 16, 2016 at 5:16
1 Answer
Reset to default 5Try this Regex which works with javascript:
https?:\/\/(?=(?:....)?amazon|smile)(www|smile)\S+(((?:\/(?:dp|gp)\/([A-Z0-9]+))?\S*[?&]?(?:tag=))?\S*?)(?:#)?(\w*?-\w{2})?(\S*)(#?\S*)+
I made a little change:
match[3] & match[4] = match[2] & match[3]
match[2] = match[4].
Hope it helps.
Demo: https://regex101./r/sT2wj8/2