Skip to content

Instantly share code, notes, and snippets.

@chrisribe
Last active August 12, 2022 19:52
Show Gist options
  • Save chrisribe/a22fb36f4e306ac941eb667b8b6e133c to your computer and use it in GitHub Desktop.
Save chrisribe/a22fb36f4e306ac941eb667b8b6e133c to your computer and use it in GitHub Desktop.
Array contains partial and exact match
var allowedURLs = [
"https://www.google.com",
"https://www.youtube.com/watch?v=mp5WJwT7HW0",
"https://www.youtube.com"
];
function arrayContains(targetArray, stringTofind, exactMatch) {
var matches = targetArray.filter(function (aItem) {
if (exactMatch)
return (stringTofind === aItem);
else
return (stringTofind.indexOf(aItem) != -1);
});
return matches;
}
var matches = arrayContains(allowedURLs, "https://www.youtube.com/watch?v=mp5WJwT7HW0");
if(matches.length > 0){
console.log('found match!')
} else{
console.log('no')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment