1

I am trying to learn if there is a javascript method to search a string to see if that string contains any of the values in an array of strings.

An example would be:

var a = 'How now brown cow';
var b = new Array('red', 'green', 'brown');

The resulting function would return true because the word brown is contained within the string a .

More specifically what I am trying to do (except using values from form input) is:

var a = '12345@gmail.com';
var b = new Array('.com', '.net', '.org');

This should also return true. Then based on this I will go on to accept var a as valid.

My actual code as of right now (which always returns null) is as follows:

function check_domain(){
  for(var i=0; i<domains.length; i++){
    var d = domains[i];
    if(elemen.value.indexOf(d) != d){
      return null;
    }
    else{
      vEmail.style.visibility = 'visible';
    }
  }
}

  window.domains = new Array(....Array values here....);
person0
  • 1,278
  • 2
  • 15
  • 20
  • BTW, you shouldn't really use the window object for storing variables. – Jivings Feb 16 '12 at 00:43
  • Fyi, the `[...]` syntax is preferred over the `new Array(...)` syntax unless you want to create a new array with a certain amount of undefined elements (in which case you would use `new Array(n)` for `n` elements). Besides that, your code leaks globals, for example, you should really use `var i` in the `check_domain()` function - you really do not want any loop variable to become global! – ThiefMaster Feb 16 '12 at 01:01
  • @ThiefMaster I fixed the `var i` issue, but the Array is not empty, i just left it empty and wrote what I did because it has over 100 strings in it that took up a lot of space irrelevant to the question. – person0 Feb 16 '12 at 01:15
  • @Jivings why not? and what should I do to avoid it. My code has a lot of this going on because I'm using the `addEventListener` method to call all functions. Any time a user clicks on the page all information about that element is stored as `elemen`; when they click something else the value of `elemen` automatically changes to the new element clicked. Should I just declare the variables as global variables and use them like that? I'm getting confused cause people keep telling me not to use global variables but I don't know how the heck to avoid it and still acomplish the purposes of my code. – person0 Feb 16 '12 at 01:24
  • Sorry PTBN :) You'd be better to just declare them with `var` at the top of scope. If you're interested in improving your JavaScript then take a look at [closures](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) and [self invoking functions](http://stackoverflow.com/questions/3720283/what-is-this-practice-called-in-javascript) to control scope. – Jivings Feb 16 '12 at 09:04

3 Answers3

2

You can create a regular expression from the array:

var re = new RegExp(domains.join('|').replace(/\./g,'\\.'));

Then you can test a string:

var a = '12345@gmail.com';
var found = re.test(a);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Could you further explain `join('|')` and `replace(/\./g,'\\.')` please? – person0 Feb 16 '12 at 01:08
  • @Proud_to_be_Noob: The `join` method makes a string from the array that looks like `".com|.net|.org"`, then the replace turns each `.` into `\.` as that is how you specify a period in a regular expression. – Guffa Feb 16 '12 at 08:42
1

Not sure why you've got .indexOf(d) != d. Shouldn't it be:

function check_domain(){
   var d, i;
   for(i = 0; i < domains.length; i++){
      d = domains[i];
      if(elemen.value.indexOf(d) != -1) {
        return true;
      }
   }
}
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • Oh yeah, haha don't know why I did that, my bad, I was thinking of a different method but wrote `indexOf` instead. Thanks, that fixed the problem. – person0 Feb 16 '12 at 00:37
0

Yes, there is something called Regular Expressions, if you really need it to match values in an array you can create a loop and check for each word against the string, something like this:

var a = 'How now brown cow';
var b = new Array('red', 'green', 'brown');

function Check(target, lookFor)
{
    for(var I = 0, L = lookFor.length; I <= L; I++)
        if(new RegExp(lookFor[I]).test(target))
            return true;
    return false;
}

alert(Check(a, b));

But in your case, the best way to go about this is just to join all the words you want to look for in the target string inside only one regular expression, so you avoid using a loop.

if(/(\.com|\.net|\.org)$/.test("sfdsdfsdf@gmail.com"))
{
// Valid email
}
else
{
// Invalid email
}
Delta
  • 4,308
  • 2
  • 29
  • 37
  • Any reason you are using uppercase loop variables? While it's mainly a matter of style, I think pretty much everyone uses lowercase `i`, `j`, `k`, `ii`, `jj` for loop variables. And I don't think caching the `.length` value is really necessary - most likely the number of elements in an array is stored so accessing it is `O(1)` just as accessing the variable where you cached it. – ThiefMaster Feb 16 '12 at 01:03
  • No reasons at all, I have been doing it like that out of habit but I don't think that is much important right? Same goes to caching the length. – Delta Feb 16 '12 at 02:23