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")
}
~str.indexOf(v). For the one who does not know: check MDN ~ Bitwise NOT Reference developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…You may use:
As from Docs:
The
includes()method determines whether one string may be found within another string, returningtrueorfalseas appropriate.
Example:
const substrings = ['connect', 'ready','gmanetwork'];
const str = 'gmanetwork.com';
if(substrings.some(s => str.includes(s))) {
console.log("Value exists");
}