检查 url 是否包含黑名单字符串 javascript

如果我有一系列列入黑名单的单词,如下所示:


banLinks = ["hello.com","test.net","this.com"];

我想检查一下myLink我的通关是否a.getAttribute("href")包含这些词之一,最好的方法是什么?现在我正在尝试这个,但它不起作用:


            for (let i = 0; i < banLinks.length; ++i) {

                if ( myLink.indexOf(banLinks[i]) <= -1 ){

                    urllink = myLink;

                }

            }

完整部分:


    var urllink;

    const banLinks = ["hello.com","test.net","this.com"];

    for (let j = 0; j < titlesArray.length; ++j) {

        if ( "some logic not important" ){


            for (let i = 0; i < banLinks.length; ++i) {

                if ( titlesArray[j].link.indexOf(banLinks[i]) <= -1 ){

                    urllink = titlesArray[j].link;

                }

            }


        }

    };


MMMHUHU
浏览 115回答 4
4回答

HUH函数

您是否处于无法使用的情况Array.prototype.findIndex?(即你必须支持IE)如果不:const banLinks = ["hello.com","test.net","this.com"];const is_banned = banLinks.findIndex(bl => myLink.indexOf(bl) > -1) > -1并使用您编辑的示例:const banLinks = ["hello.com","test.net","this.com"];// is_banned is now a function that ingests a link.const is_banned = (myLink) => banLinks.findIndex(bl => myLink.indexOf(bl) > -1) > -1// Get first link from titlesArray that is not banned.const urllink = titlesArray.map(t => t.link).find(li => is_banned(li) == false)尽管这都是根据您提供的代码进行的猜测。目前还不清楚你想在 for 循环中做什么。如果要查找第一个有效的(即,未禁止的)urllink,您应该break在找到它后才进行。否则,urllink其余的后续有效值titlesArray将覆盖先前的有效值。

大话西游666

告别 indexOf() 并使用 ES7 .includes() 来检查某个项目是否在数组内。我想检查我通过 a.getAttribute("href") 获得的 myLink 是否包含这些单词之一,最好的方法是什么?if ( banLinks.includes(myLink) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; urllink = myLink;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

蓝山帝景

你可以试试这个。请看一下const banLinks = ["hello.com","test.net","this.com"];var bannedUrlTest = new URL("http://www.hellotest.net");var safeUrlTest = new URL("http://www.google.net");var safeUrl = null;var bannedUrlArray = banLinks.filter((link)=>{&nbsp; return bannedUrlTest.href.includes(link)&nbsp;&nbsp;})if(bannedUrlArray.length < 1){&nbsp; safeUrl = bannedUrlTest.href;} else {&nbsp; console.log('contains banned links')}var safeUrlArray = banLinks.filter((link)=>{&nbsp; return safeUrlTest.href.includes(link)&nbsp;&nbsp;})if(safeUrlArray.length < 1){&nbsp; safeUrl = safeUrlTest.href;&nbsp; console.log('safeUrl', safeUrl);}

慕的地10843

<html>&nbsp; <body>&nbsp; &nbsp; <ul class="links">&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="hello.com">hello</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="steve.tech">steve</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="this.net">this</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <a href="Nathan.net">Nathan</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul>&nbsp; &nbsp;&nbsp; </body>&nbsp; <script>&nbsp; &nbsp; &nbsp; var links=[...document.querySelectorAll(".links a")]; //all links&nbsp; &nbsp; &nbsp; const BlackListLinks = ["hello.com","test.net","this.com"]; //BlackListLinks&nbsp; &nbsp; &nbsp; const goodLinks=[]; //stores the links don't in BlackListLinks&nbsp; &nbsp; &nbsp; for (let i = 0; i < links.length; i++) {&nbsp; //iterate over all links&nbsp; &nbsp; &nbsp; &nbsp; const link = links[i].getAttribute("href"); //get the current link&nbsp; &nbsp; &nbsp; &nbsp; if (!BlackListLinks.includes(link)){ //test if the current link don't exist in BlackListLinks&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goodLinks.push(link); //add link to the goodLinks&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; console.log(goodLinks);//["steve.tech", "this.net", "Nathan.net"]&nbsp; </script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript