9

I've two variables:

var input = "[email protected]";
var preferredPatterns = ["*@gmail.com", "*@yahoo.com", "*@live.com"];

Want to match the input with preferred pattern array. If any of the patterns matches I've to do certain task (in this example, input is a definite match). How can I match against an array of pattern in javascript?

4
  • 1
    What kind of patterns are those? Those are not regular expressions. Commented Apr 6, 2015 at 7:07
  • how can I turn them into?
    – fatCop
    Commented Apr 6, 2015 at 7:11
  • You need to take every and rewrite. Do you know regular expressions syntax?
    – zerkms
    Commented Apr 6, 2015 at 7:11
  • Not to mention, [email protected] is not a string, either.
    – Amadan
    Commented Apr 6, 2015 at 7:13

7 Answers 7

9

You can compile your patterns (if they are valid regular expressions) into one for performance:

var masterPattern = new RegExp(patterns.join('|'));

Putting it all together:

var input = '[email protected]';
var preferredPatterns = [
  ".*@gmail.com$",
  ".*@yahoo.com$",
  ".*@live.com$"
];

var masterPattern = new RegExp(preferredPatterns.join('|'));

console.log(masterPattern.test(input));
// true
3
  • That was not a part of the question. If you want that, you're better off going with @AvinashRaj's solution. This will only find whether or not any of the pattern matches, as per your original question, very very fast. (Handcrafting the masterPattern would be even faster, as you can refactor the .* -- $ bit for more speed).
    – Amadan
    Commented Apr 6, 2015 at 7:21
  • but this one shows SyntaxError: Invalid regular expression: /*@qa-server5.com$|*@qa-server4.com$/
    – fatCop
    Commented Apr 6, 2015 at 8:36
  • "if they are valid regular expressions". Yours are not - there needs to be a period in front of the *. If you want to allow your users to input patterns, then you also need a method to transform them into valid regular expressions (escaping any dangerous characters, and turning * into .*, for starters). Otherwise, just write correct regular expression patterns in the first place.
    – Amadan
    Commented Apr 6, 2015 at 8:38
4

You need to use RegExp constructor while passing a variable as regex.

var input = '[email protected]';
var preferredPatterns = [".*@gmail\\.com$", ".*@yahoo\\.com$", ".*@live\\.com$"];
for (i=0; i < preferredPatterns.length;i++) {
  if(input.match(RegExp(preferredPatterns[i]))) {
     console.log(preferredPatterns[i])
    }
    }

Dot is a special meta-character in regex which matches any character. You need to escape the dot in the regex to match a literal dot.

As @zerkms said, you could use the below list of patterns also.

var preferredPatterns = ["@gmail\\.com$", "@yahoo\\.com$", "@live\\.com$"];
4
  • you mean that i need to give an exact pattern to match email id? Commented Apr 6, 2015 at 7:13
  • No, I mean that you can remove .* from regular expressions and nothing will change.
    – zerkms
    Commented Apr 6, 2015 at 7:14
  • 1
    Use dot before star in your Regex. Please recheck the Regex I used. Commented Apr 6, 2015 at 8:44
  • .* will match all the characters including space. So we should use non space character \S i.e. ["\\S*@gmail\\.com", "\\S*@yahoo\\.com", "\\S*@live\\.com"] Commented Feb 25, 2019 at 5:15
1

Try this helper function:

/**
 * Returns an integer representing the number of items in the patterns 
 * array that contain the target string text
 */
function check(str, patterns) {
   return patterns.reduce(function (previous, current) {
      return previous + (str.indexOf(current) == -1 ? 0 : 1);
    }, 0);
}

check("[email protected]", ["@gmail.com", "@yahoo.com", "@live.com"]; // returns 1
check("[email protected]", ["@gmail.com", "@yahoo.com", "@live.com"]; // returns 0
1

If you want a general approach to matching against a list of regular expressions then some version of Avinash Raj's answer will work.

Based on the fact that you are specifying certain domains, you might want to match any valid email address using the regex here, and if it matches then check if the domain is a preferred one. There are a number of different ways you could do that of course. Here's just a simple example, splitting on the @ and using jQuery.inArray() to check if the domain is preferred.

var preferredDomains = ["gmail.com", "yahoo.com", "live.com"];

function isValid(inputVal) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

    return re.test(inputVal) && $.inArray(inputVal.split('@')[1], preferredDomains) > -1;
}

The advantage here is that the underlying regex doesn't change, just the much easier to read/maintain list of domains. You could tweak this to capture the domain in a group, instead of using split().

0

No regexp you may do as follows;

function matchPattern(xs, [y,...ys]){
  
  function helper([x,...xs]){
    return "*" + xs.join('') === y ? true
                                   : xs.length ? helper(xs)
                                               : false;
  }
  
  return helper(xs) ? y
                    : ys.length ? matchPattern(xs,ys)
                                : "No Match..!";
}

var input             = "[email protected]",
    preferredPatterns = ["*@yahoo.com", "*@live.com", "*@gmail.com"];
    result            = matchPattern(input, preferredPatterns);

console.log(result);

0
preferredPatterns.forEach(function(element, index){ 
   if(input.match('/'+element) != null){ 
      console.log('matching  ' + element)  
   }
})

you can write your custom logic if a string matches any pattern.

-2

You may iterate through array and then use regex to compare with individual items.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.