[英]How can I perform something similar to forEach() but using next line?
If I use the following code:如果我使用以下代码:
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"
assuming that each new line is \\n of course假设每个新行都是 \\n
and I run this code:我运行这个代码:
let dorks = original.split('/').pop().split('?')[0];
console.log(dorks)
it only returns:它只返回:
index.php (as for the first url) index.php(至于第一个网址)
How can I make it return for each line all together?我怎样才能让它为每一行都返回?
Here's a forEach
that'll do it for you:这是一个
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,'')));
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"
] */
the /\\s/
is a Regular Expression which detects all kinds of white spaces. /\\s/
是一个正则表达式,可以检测各种空格。 After creating an array from your string by splitting at the positions of all white spaces you can then split again each value by the '?'
通过在所有空格的位置拆分从字符串创建数组后,您可以再次通过
'?'
拆分每个值'?'
which you already did.你已经这样做了。
Make sure the scheme of the string is exactly like that.确保字符串的方案完全一样。 Otherwise this script might throw an error.
否则此脚本可能会引发错误。
I'm not entirely certain how you wish to display the matches but here is how you can avoid looping:我不完全确定您希望如何显示匹配项,但您可以通过以下方式避免循环:
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(' '));
There are several way to obtain what you want.有几种方法可以获得你想要的东西。 Assuming the minimal change of code, your problem is just apply your logic on multiple lines.
假设代码更改最少,您的问题只是在多行上应用您的逻辑。
So the easiest way is just split the strings using the new line as delimiter, and then apply for each of them the code you already wrote:因此,最简单的方法是使用新行作为分隔符拆分字符串,然后为每个字符串应用您已经编写的代码:
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)
Notice the usage of the template literals in order to easily got the new line.请注意模板文字的用法,以便轻松获取新行。
As you can see the main logic is exactly the same you wrote ( url.split("/").pop().split("?")[0]
) it's just applied for each line using map .正如您所看到的,主要逻辑与您编写的完全相同(
url.split("/").pop().split("?")[0]
)它只是使用map应用于每一行。
You might solve this issue using a regular expression, but I think it would be beneficial to you understand how to apply the same logic on multiple line (therefore you can easily change the function you pass to map
if the logic changes.您可以使用正则表达式来解决这个问题,但我认为这对您了解如何在多行上应用相同的逻辑很有帮助(因此,如果逻辑发生变化,您可以轻松更改传递给
map
的函数。
let mapped = original.split('\n').map(o => o.split('/').pop().split('?')[0])
工作正常,我在新行拆分并映射了函数!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.