我正在尝试构建完美的亚马逊链接正则表达式,以便在javascript中使用。这就是我到目前为止所做的:
var reg = /https?:\/\/(www|smile)\.amazon\.com\/(?:(?:[\w-]+\/)?(?:dp|gp\/product)\/(\w{10})\/)?/;
我希望这与以下所有网址完全匹配:
http://smile.amazon.com/dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
http://www.amazon.com/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.com/gp/redirect.html?ie=UTF8&location=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fsubscribe-and-save%2Fmanager%2Fviewsubscriptions%3Fie%3DUTF8%26ref_%3Dya%255FT15%255F33&tag=menasheh02-20&linkCode=ur2&camp=1789&creative=390957
http://www.amazon.com/gp/student/signup/info?ie=UTF8&refcust=7EATHY4IXOFTTEMLIHVC3YL6DI&ref_type=generic
http://www.amazon.com/gp/video/primesignup?tag=menasheh02-20
https://smile.amazon.com/dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
https://smile.amazon.com/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.com/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.com/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.com/dp/B0005ZH4QI/?tag=menasheh02-20&psc=1&smid=ATVPDKIKX0DER
https://www.amazon.com/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.com/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.com/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.google.com/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.com/B53C945A8D?randomstuff=34341&otherrandomstuff=2
现在,它不匹配任何一个坏的 - 这部分相对简单。 (它也不匹配其中的gp/redirect.html?
网址。)棘手的部分是让匹配分别返回网址的每个有用部分,特别是考虑if / elses和#。
#Working#
match[1]
应该等于" www"或者"微笑。"
match[2]
应该等于ASIN,如果网址没有/dp/%ASIN%
,%SEO-string%/dp/%ASIN%
或/gp/product/%ASIN%
match[3]
应该等于.com之后的其余网址,或者在产品之后如果设置了ASIN,但最后不包括#
match[4]
应该从匹配[3]的开头到tag=
相等,如果存在的话。
match[5]
应该等于tag参数(如果存在)
match[6]
应该等于tag参数之间的其余url(如果存在;否则为空)和#(如果存在,否则结束)
match[7]
应该等于最后的#和后面的任何内容,如果没有
我只是进入更复杂的正则表达式,如果有#等等,就会陷入不会一直走到行尾的事情。
有人可以获得更多经验吗?感谢。
答案 0 :(得分:3)
尝试使用适用于javascript的正则表达式:
https?:\/\/(?=(?:....)?amazon|smile)(www|smile)\S+com(((?:\/(?:dp|gp)\/([A-Z0-9]+))?\S*[?&]?(?:tag=))?\S*?)(?:#)?(\w*?-\w{2})?(\S*)(#?\S*)+
我做了一点改变:
匹配[3]&匹配[4] =匹配[2]&匹配[3]
匹配[2] =匹配[4]。
希望它有所帮助。