-1

I'd like to use a regex to determine if a user-supplied value exists in a list of approved values, regardless of case. Here is a pared-down example of the current JavaScript code, which works to match "JAN", "Jan", and "jan"–but does not match "jAN", "jAn", etc:

var validateValue = function(field, patternName){
    "use strict"; //let's avoid tom-foolery in this function
    var pattern="";

    switch(patternName)
    {
    case "MMM": //month names only
        pattern=/^JAN|Jan|jan*$/;
        break;
    // other cases  and default follow in real code
    }

    if ( (!field.value.length) || pattern.test(field.value) ){
        //we're good (the field is blank or passes the regular expression test); remove field's error message, enable the submit button
    }
    else {
        //problems; let's show the error message and put focus back on problem field, disable the submit button
    }
};

I tried pattern=/^(?i)JAN|Jan|jan*$/; based on what I learned from "Case insensitive Regex without using RegexOptions enumeration", but that doesn't do the trick ("Uncaught SyntaxError: Invalid regular expression...")

What is the correct regular expression for evaluating if a value matches, case-insensitive, a list item?

Community
  • 1
  • 1
Jeromy French
  • 11,812
  • 19
  • 76
  • 129

2 Answers2

10

You can use the i (case insensitive) modifier like so:

pattern = /^jan*$/i; // <-- it goes at the end

Another way to define regular expressions is with the RegExp object:

pattern = new RegExp("^jan*$", "i");

I find this form to be more readable.


Also keep in mind that /^jan*$/i will match things like:

JAN
jannnn
jannNN
jAn

I'm not sure if that is what you want.


If you just want to match a predefined set you could opt for a non-regular-expression solution:

function isMonth(value) {
    var months = "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec".split("|");
    return months.indexOf(value.toLowerCase()) !== -1;
}
Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • with the i on the end wouldn't he just need pattern=/^jan*$/i; ? – Decker W Brower Apr 04 '13 at 15:30
  • Hmm..just looking for JAN, FEB, MAR...etc. So current pattern will be `pattern=/^(?i)jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec*$/i;`? – Jeromy French Apr 04 '13 at 15:32
  • based on `pattern=/^(?i)jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec*$/i;`, would "DeCxyz" be a match? – Jeromy French Apr 04 '13 at 15:34
  • I don't think `DeCxyz`, but `DeCccccc` will. – Halcyon Apr 04 '13 at 15:36
  • Thanks for the non-regex alternative, Frits, but this function uses regex for other patterns (email, phone, name). – Jeromy French Apr 04 '13 at 15:36
  • I would discourage overzealous use of regular expressions. Phonenumbers and names are very hard to regexp match. E-mail is possible but I wouldn't worry about it too much since it's so easy to come up with an email address that _looks_ valid but isn't. Regular expressions is not the holy grail for getting people to enter valid information. – Halcyon Apr 04 '13 at 15:38
  • Understood. The idea behind this function is to assist users to enter valid search criteria. It's not a security mechanism--we handle that in other ways--it's more of a convenience function. If a user outsmarts the regex, they're only wasting their own time. – Jeromy French Apr 04 '13 at 15:41
  • Sure, but if it's just a filter it doesn't matter what the user enters so long as it doesn't break your queries. If a user enters "puppy" as a phone number I don't see why she couldn't. – Halcyon Apr 04 '13 at 15:43
3

If you don't want to use regular expression options, you can try this regex :

pattern = /^[Jj][Aa][Nn]$/
Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53