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

Check if url contains blacklisted strings 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;
                }
            }

        }
    };

告别 indexOf() 并使用 ES7 .includes() 来检查一个项目是否在数组中。

I'd like to check if myLink I got through a.getAttribute("href") contains one of those words what'd be the best way to go?

if ( banLinks.includes(myLink) ){
                    urllink = myLink;
                }

您是否处于无法使用 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。否则,来自 titlesArray 其余部分的后续有效 urllink 将覆盖较早的

你可以试试这个。请看一下

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)=>{
  return bannedUrlTest.href.includes(link)  
})

if(bannedUrlArray.length < 1){
  safeUrl = bannedUrlTest.href;
} else {
  console.log('contains banned links')
}

var safeUrlArray = banLinks.filter((link)=>{
  return safeUrlTest.href.includes(link)  
})

if(safeUrlArray.length < 1){
  safeUrl = safeUrlTest.href;
  console.log('safeUrl', safeUrl);
}

<html>
  <body>
    <ul class="links">
      <li>
        <a href="hello.com">hello</a>
      </li>
      <li>
        <a href="steve.tech">steve</a>
      </li>
      <li>
        <a href="this.net">this</a>
      </li>
      <li>
        <a href="Nathan.net">Nathan</a>
      </li>
    </ul>   
  </body>
  <script>
      var links=[...document.querySelectorAll(".links a")]; //all links
      const BlackListLinks = ["hello.com","test.net","this.com"]; //BlackListLinks
      const goodLinks=[]; //stores the links don't in BlackListLinks
      for (let i = 0; i < links.length; i++) {  //iterate over all links
        const link = links[i].getAttribute("href"); //get the current link
        if (!BlackListLinks.includes(link)){ //test if the current link don't exist in BlackListLinks
          goodLinks.push(link); //add link to the goodLinks 
        }
    }
      console.log(goodLinks);//["steve.tech", "this.net", "Nathan.net"]
  </script>
</html>