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!

3
  • 1
    Let me introduce you to the for loop. :) Commented Oct 22, 2013 at 21:29
  • possible duplicate of jQuery or JavaScript strstr Commented Oct 22, 2013 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 Commented Oct 22, 2013 at 21:31

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

indexOf is better unless you're using regular expressions
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"]

2 Comments

don't forget to escape your regex... new RegExp(urls.join('|').replace(/[\.\^$*+?()[{\\]/g,'\\$&'),'g').test(currentHref)
@zamnuts one more reason why for-way is preferred :)
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.

Comments

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);

Comments

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.

Comments

-1

Make a foreach and then check indexOf ;)

2 Comments

There is no foreach in JavaScript.
@undefined technically yes, Array.forEach, although this isn't best for this use case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.