3

I have an array which lists a couple of websites:

var validSites = new Array();
validSites[0] = "example_1.com";
validSites[1] = "example_2.com";
validSites[2] = "example_3.com";

now i have a small script which checks what web address you are on and could return something like this:

example_1.com/something/something_else

now i need to check if that address is one of the valid sites. so

example_1.com/*ANYTHING* 

would pass as correct. but

exampleshmample.com

would pass as incorrect.

Now i know you can do an indexOf() which can check if a string is part of a string and it would return -1 if false. but how would i check it through the entire array?

P.s - its for a Chrome Extension.

thanks

2
  • If any of the answers have helped you in solving your problem ensure you mark that answer as accepted to indicate to future users what has worked for you. Commented Apr 2, 2013 at 9:45
  • It greatly benefits the community at large to mark an answer below as accepted if it has solved your issue. The respondents took time out to suggest an answer, it would be very courteous of you to take a few moments to recognize their contribution. Commented Jul 17, 2013 at 20:05

4 Answers 4

5

Here’s an idea:

var str = 'example_1.com/something/something_else';
if( validSites.indexOf( str.split('/')[0] ) > -1 ) {
    // is valid
}

Another one is to use regexp on a joined array:

var str = 'example_1.com/something/something_else';
new RegExp('^('+validSites.join('|')+')','i').test(str);

This will also match f.ex example_1.comyoyoyo

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

2 Comments

I think he wants any valid substring, not just components. Or at least thats how he phrased it.
Added an example using regexp for more fine control over the match.
1
if (validStates.indexOf("example_1.com") > -1) {
  // Then it's inside your array
} 
else {
  // Then it's not inside your array
}

2 Comments

I'd suggest !indexOf() instead.
he's not asking for exact matches, he's asking for substrings.
0

I'd go with json notation, if you can switch from an array, in this scenario

var validSites = {
  "example_1.com":"valid",
  "example_2.com":true,
  "example_3.com":1 //you could even start putting paths in here to beef up your check.
};

//..your check function would be: .....

if(validSites[window.location.hostname]){
  return true;
}else{
  return false;
}

1 Comment

obviously, if you can't switch from an array this isn't possible.
0

You can achieve this by looping and setting flag variable, Try this, i am not tested this. Just i typed the code directly. i think this may help you

var flag =0;
var givenurl='example_1.com/*ANYTHING*';
for(int i=0 i<validSites.length;i++){
if(givenurl.indexOf(validSites[i])){
flag=1 //Found
}
}
if(flag) { //Url Found }else{ //not found } 

Comments

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.