最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression list matching without case sensitivity - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 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;
}

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

pattern = /^[Jj][Aa][Nn]$/
发布评论

评论列表(0)

  1. 暂无评论