10

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? Commented Apr 6, 2015 at 7:11
  • You need to take every and rewrite. Do you know regular expressions syntax? Commented Apr 6, 2015 at 7:11
  • Not to mention, [email protected] is not a string, either. Commented Apr 6, 2015 at 7:13

7 Answers 7

10

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
Sign up to request clarification or add additional context in comments.

3 Comments

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).
but this one shows SyntaxError: Invalid regular expression: /*@qa-server5.com$|*@qa-server4.com$/
"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.
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 Comments

you mean that i need to give an exact pattern to match email id?
No, I mean that you can remove .* from regular expressions and nothing will change.
Use dot before star in your Regex. Please recheck the Regex I used.
.* 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"]
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

Comments

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().

Comments

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

Comments

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.

Comments

-2

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

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.