简体   繁体   中英

Checking if a link contains a word in array javascript

How to check if a link contains a word in array?

var substrings = ['connect', 'ready','gmanetwork'];
var str = window.location.href
if (substrings.some(function(v) { return str === v; })) {
   alert("true")
}

I would use str.indexOf(v) > -1 to check if the href contains the word in your array.

If you want to be one of the cool kids you can use the Bitwise NOT like this. ~str.indexOf(v)

 var substrings = ['connect', 'ready','gmanetwork']; // var str = window.location.href var str = 'somethingconnect.com' if (substrings.some(function(v) { return str.indexOf(v) > -1 })) { console.log("true") } 

You may use:

As from Docs :

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

Example:

 const substrings = ['connect', 'ready','gmanetwork']; const str = 'gmanetwork.com'; if(substrings.some(s => str.includes(s))) { console.log("Value exists"); } 

 var substrings = ['connect', 'ready','gmanetwork']; var str = 'http://www.example.com/connect-your-pc'; //url if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) { alert("true"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM