4 回答
TA贡献1886条经验 获得超2个赞
这是一个forEach可以为你做的:
//note the use of a backtick here to allow your new line characters in string
let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest`
//split on newline, and then for each URL, grab everything before the ? and trim extra spaces
original.split("\n").forEach((url)=> console.log(url.split("?")[0].replace(/ /g,'')));
TA贡献1995条经验 获得超2个赞
有多种方法可以获得您想要的东西。假设代码更改最少,您的问题只是将逻辑应用在多行上。
因此,最简单的方法就是使用新行作为分隔符分割字符串,然后为每个字符串应用您已经编写的代码:
let s = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest`;
let dorks = s.split("\n").map(url => url.split('/').pop().split('?')[0]);
console.log(dorks)
请注意模板文字的用法,以便轻松获取新行。
正如您所看到的,主要逻辑与您编写的 () 完全相同,它只是使用mapurl.split("/").pop().split("?")[0]
应用于每一行。
map
您可以使用正则表达式来解决这个问题,但我认为这将有助于您了解如何在多行上应用相同的逻辑(因此,如果逻辑发生变化,您可以轻松更改传递给的函数。
TA贡献2080条经验 获得超4个赞
我不完全确定您希望如何显示匹配项,但以下是避免循环的方法:
let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php?instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest`;
let re = /[^.\/]+\.(?:php|asp)/g;
console.log(original.match(re));
console.log(original.match(re).join(' '));
TA贡献1817条经验 获得超14个赞
let original = `http://www.spur-g-shop.de/index.php?action=buy_now&BUYproducts_id=56&
http://associations.beauvais.fr/en-un-clic/index.php?option=com_fabrik&view=list&listid=4&Itemid=520&asso_annuaireweb___id_soustheme_raw=40&sous_theme___ID_Theme=SPORTS&resetfilters=1
http://laptopbank.net/product_detail.php?detail_id=B075FLBJV7
https://www.music-scores.com/sheet-music/freeinstrument.php? instrument=Alto%20Sax
http://www.traxjo.com/index.php?PageType=2&MenuID=1&Sub=1&Lang=1
http://www.bizfocus.co.kr/admin/bbs/down.php?code=data&idx=8928&no=1
http://www.vivitekthailand.com/en/Product_view.php?ProductId=115&CategoryId=7
http://www.fsrm.ch/doc/c474.php?lang=e&id=474
https://catalog.prm-catalog.com/index.php?lang=tw&ID=18&CatalogID=2839
https://astacology.org/AboutCrayfish.asp?uid=Guest`
original.split(/\s/).map(w => w.split('?')[0])
/* returns [
"http://www.spur-g-shop.de/index.php",
"http://associations.beauvais.fr/en-un-clic/index.php",
"http://laptopbank.net/product_detail.php",
"https://www.music-scores.com/sheet-music/freeinstrument.php",
"http://www.traxjo.com/index.php",
"http://www.bizfocus.co.kr/admin/bbs/down.php",
"http://www.vivitekthailand.com/en/Product_view.php",
"http://www.fsrm.ch/doc/c474.php",
"https://catalog.prm-catalog.com/index.php",
"https://astacology.org/AboutCrayfish.asp"
] */
这/\s/是一个检测各种空白的正则表达式。通过在所有空格的位置进行拆分来从字符串创建数组后,您可以按照'?'您已经执行的操作再次拆分每个值。
确保字符串的方案与此完全相同。否则这个脚本可能会抛出错误。
添加回答
举报