简体   繁体   English

如何执行类似于 forEach() 但使用下一行的操作?

[英]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.

相关问题 我怎样才能实现类似于 javascript 中的字符串指针的东西? - How can I achieve something similar to a pointer to a string in javascript? 如何使用 Javascript 和 HTML 对多行执行带有按钮的表单操作? - How can I perform a form action with a button next to it for multiple rows using Javascript and HTML? 科尔多瓦android应用程序如何使用线条并在函数中使用该信息来测量图片中的某些内容 - Cordova android app how can I measure something in a picture using a line and using that information on a function jQuery的下一个选择器或类似的东西 - JQuery next selector or something similar 如何使用BroadcastChannel API或Safari 10+中的类似功能? - How can I use the BroadcastChannel API or something similar in Safari 10+? 如何使用与此类似的东西将移动键从箭头键更改为 WASD? - How would I change the movement keys to WASD from the arrow keys using something similar to this? 我可以以某种方式保存带有JSON或类似内容的循环数据结构吗? - Can I somehow save circular data structures with JSON or something similar? 我可以在过滤器 function 之后进行回调(或类似的事情)吗? - Can i do a callback (or something similar) after a filter function? 我可以在 pyscript 中使用类似于 Angular 的 *ngFor 的东西吗? - Can I use something similar to Angular's *ngFor in pyscript? 我可以在 node.js 的交换机上使用类似的东西吗? - Can i use something similar on the switch with node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM