JVG
JVG

Reputation: 21150

Javascript: Check if any elements in an array contain a part of a string

I have an array of "banned domains", and I'm trying to find an easy way to check if a particular email is from one of those domains. Consider the following:

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"]
if(bannedDoms.indexOf(email.split("@")[1]) === -1){
   // It's a good domain!
}

This works fine, except for the last example, as the salesforce emails are from weird domains like emailtosalesforce@t-1l9sefi2sef5wlowk59bvm0uuh78mkdfuhioamfu7vxv8agjjh.o-h7zieac.na17.le.salesforce.com - the common factor being that they all have .le.salesforce.com in the address.

Searching via Array.prototype.indexOf() is quite an elegant solution and I'd like to use something similar if possible - but to also catch parts of strings, rather than a whole string match.

What's the most efficient and simple way to do this in Javascript?

Upvotes: 1

Views: 1225

Answers (4)

Thomas Junk
Thomas Junk

Reputation: 5676

I would go for a regex:

/gmail\.com|hotmail\.com|\le\.salesforce\.com/.test("emailtosalesforce@t-1l9sefi2sef5wlowk59bvm0uuh78mkdfuhioamfu7vxv8agjjh.o-h7zieac.na17.le.salesforce.com")

A fiddle is here

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];
r=new RegExp(bannedDoms.map(function(x){ return x.replace(/\./g,'\\.') }).join("|"));

Explanation:

You could simply build up a regular expression, taking each banned domain and combine them with or. The simplest form would be a|b, read a or b. In principle gmail.com or hotmail.com would become gmail.com|hotmail.com - with one exception: . is a special character in a regular expression meaning any character. To cirumvent this, we need to escape the dot to \..

r=new RegExp(bannedDoms.map(function(x){ return x.replace(/\./g,'\\.') }).join("|"));

Array.prototype.map() is a function, which applies a function onto each element of an array - returning the resulting array.

The map-part does nothing more than replacing every occuring . with an escaped version \..

Array.prototype.join() joins the resulting array-elements with a pipe = or.

Thanks @torazaburo

Upvotes: 1

Sachin
Sachin

Reputation: 978

I think you'd have to do a string indexOf, if you want to catch those. So, You can try something like this..

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"]
for(var i=0; i<bannedDoms.length; i++) {
    if(email.indexOf(bannedDoms[i]) === -1){
         // It's a good domain!
    }
}

Upvotes: 0

Vidul
Vidul

Reputation: 10528

Probably a good candidate for the endsWith simulation:

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];
var email1 = "[email protected]";
var email2 = "[email protected]";

console.log(bannedDoms.some(function (e) {
    return email1.indexOf(e, email1.length - e.length) !== -1;
})); // true

console.log(bannedDoms.some(function (e) {
    return email2.indexOf(e, email2.length - e.length) !== -1;
})); // false

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

I think you will have to iterate and test the domains like

var bannedDoms = ["gmail.com", "hotmail.com", ".le.salesforce.com"];

function isBanned(email) {
  var domain = email.split("@")[1];

  var banned = bannedDoms.some(function(value) {
    return domain.indexOf(value) > -1;
  })
  if (!banned) {
    // It's a good domain!
  }

  snippet.log(email + ' : ' + banned);
}

isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
isBanned('[email protected]');
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Upvotes: 0

Related Questions