This should work for you.
Changed to using test
No need for the global "g" flag as you are testing the whole string from beginning "^" to end "$"
Changed string regexPattern
into a RegExp object
The "i" flag is needed because you want case insensitive.
Javascript
var shouldMatch = "may";
var regexPattern = /^Jan$|^Feb$|^Mar$|^Apr$|^May$|^Jun$|^Jul$|^Aug$|^Sep$|^Oct$|^Nov$|^Dec$/i;
if(regexPattern.test(shouldMatch)) {
alert(shouldMatch);
}
On jsfiddle
You could also make it a little shorter and a little less ugly by doing this
var regexPattern = /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$/i;
On jsfiddle
As an alternative to regex, you could also use String.indexOf and Array.some, and try each pattern to see if it is in the string you are testing. This example will require a modern browser or a "shim"/"polyfill" for older browsers. You could also check equality "===" if you want to match the whole string rather than see if it is contained in.
Javascript
var shouldMatch = "may";
var patterns = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec".split("|");
var matched = patterns.some(function (pattern) {
if (shouldMatch.toLowerCase().indexOf(pattern.toLowerCase()) !== -1) {
alert(shouldMatch);
return true;
}
return false;
});
On jsfiddle