1

I've already read a few questions here but can't seem to find exactly what I'm looking for. Basically, I need to know if any of the elements present in an array are present in a string. An example:

var currentHref = "http://www.vorcu.com/hello";
var urls = [ 
    "vorcu.com",
    "neutronico.com",
    "word2.com"
       ];

So, I need to know if any of the elements in "urls" coincide with any of the text present in "currentHref". Any ideas?

Thanks!

user906379
  • 107
  • 9
  • 1
    Let me introduce you to the [for loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for). :) – epascarello Oct 22 '13 at 21:29
  • possible duplicate of [jQuery or JavaScript strstr](http://stackoverflow.com/questions/7015181/jquery-or-javascript-strstr) – scrowler Oct 22 '13 at 21:29
  • You're going to have to loop through the array. It might sound ugly, but I'd be surprised (happily) if there was another way – baash05 Oct 22 '13 at 21:31

6 Answers6

2

Loop through the array and use search:

str.search(arrItem)

will return the position of the match, or -1 if it isn't found.

Anthony
  • 648
  • 1
  • 7
  • 22
2

Something like this:

for (var i = 0; i < urls.length; i++) {
    if (currentHref.indexOf(urls[i]) != -1) {
        alert(urls[i]); // found occurence, break or continue
    }
}

Or RegExp way (above way is preferred):

var isInArray = RegExp(urls.join('|')).test(currentHref); // true|false

or

var matches = currentHref.match(urls.join('|')); // array of matches ["vorcu.com"]
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

Everyone is providing the Javascript answer (which I fully support), but just because you mention jQuery I figure I'd provide an alternative:

var curHref = window.location.hostname.split('.'),
    // i imagine your static string was just for test purposes
    checkHref = curHref[1]+'.'+curHref[2],
    urls = [ 
        "vorcu.com",
        "neutronico.com",
        "word2.com"
    ];

if($.inArray(checkHref,urls) > -1){
    // its in there, do your magic
}

This creates the object I assume you wanted to check (hostname minus www) and if it matches any of the items in urls using the jQuery inArray method.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
0

A function like the following should work:

function containsAny(str, arr) {
    for (var i = 0; i < arr.length; i++) {
        if (str.indexOf(arr[i]) !== -1) {
            return true;
        }
    }
    return false;
}

Alternately you could use the slower but more terse:

RegExp(arr.join('|')).exec(str);
olleicua
  • 2,039
  • 2
  • 21
  • 33
0

You could use the some method, which is supported by many browsers:

var currentHref = "http://www.vorcu.com/hello";
var urls = [ 
    "vorcu.com",
    "neutronico.com",
    "word2.com"
       ];
var containsAny = urls.some(function(x) { return currentHref.indexOf(x) >= 0; });

For compatibility with browsers where some is not supported, the MDN article I've linked provides an implementation you can use.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
-1

Make a foreach and then check indexOf ;)

Wikunia
  • 1,564
  • 1
  • 16
  • 37