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?
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?
Share Improve this question edited May 23, 2017 at 12:29 CommunityBot 11 silver badge asked Apr 4, 2013 at 15:27 Jeromy FrenchJeromy French 12.1k19 gold badges78 silver badges135 bronze badges2 Answers
Reset to default 10You 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;
}
If you don't want to use regular expression options, you can try this regex :
pattern = /^[Jj][Aa][Nn]$/