Make regular expression case insensitive in ASP.NET RegularExpressionValidator

后端 未结 5 2002
暗喜
暗喜 2020-11-27 19:40

Given this regular expression: \"^[0-9]*\\s*(lbs|kg|kgs)$\" how do I make it case insensitive? I am trying to use this in a .net regular expression validator,

5条回答
  •  渐次进展
    2020-11-27 20:10

    Use RegEx Options.

    Regex regExInsensitive = new Regex(@"^[0-9]\s(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
    

    In other languages you can usually specify a RegEx modifier after the end of the Reg Ex; the 'case insensitive' modifier is 'i':

    In Perl:

    if($var =~ /^[0-9]\s(lbs|kg|kgs)$/i) { # the /i means case insensitive
        # ...
    }
    

    In PHP:

    if(preg_match("/^[0-9]\s(lbs|kg|kgs)$/i", $var)) {
        // ...
    }
    

提交回复
热议问题