4 回答

TA貢獻(xiàn)1886條經(jīng)驗 獲得超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貢獻(xiàn)1995條經(jīng)驗 獲得超2個贊
有多種方法可以獲得您想要的東西。假設(shè)代碼更改最少,您的問題只是將邏輯應(yīng)用在多行上。
因此,最簡單的方法就是使用新行作為分隔符分割字符串,然后為每個字符串應(yīng)用您已經(jīng)編寫的代碼:
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]
應(yīng)用于每一行。
map
您可以使用正則表達(dá)式來解決這個問題,但我認(rèn)為這將有助于您了解如何在多行上應(yīng)用相同的邏輯(因此,如果邏輯發(fā)生變化,您可以輕松更改傳遞給的函數(shù)。

TA貢獻(xiàn)2080條經(jīng)驗 獲得超4個贊
我不完全確定您希望如何顯示匹配項,但以下是避免循環(huán)的方法:
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貢獻(xiàn)1817條經(jīng)驗 獲得超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/是一個檢測各種空白的正則表達(dá)式。通過在所有空格的位置進(jìn)行拆分來從字符串創(chuàng)建數(shù)組后,您可以按照'?'您已經(jīng)執(zhí)行的操作再次拆分每個值。
確保字符串的方案與此完全相同。否則這個腳本可能會拋出錯誤。
添加回答
舉報