Make regular expression case insensitive in ASP.NET RegularExpressionValidator

后端 未结 5 2010
暗喜
暗喜 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条回答
  •  旧时难觅i
    2020-11-27 20:07

    Can we make Regex case-insensitive in C#? : Yes

    Set option inline via (?i) pattern construct or via RegexOptions.IgnoreCase param

    Can we make Regex case-insensitive in JavaScript? : Yes

    Set flag via /pattern/flags syntax or the insensitive flag /REGEX/i

    (Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute? : No

    As Chris R. Timmons points out on RegularExpressionValidator doesn't ignore case:

    There is no property in the RegularExpressionValidator control that allows regex options to be set.

    If your control does both client-side and server-side validation, the regex must use a subset of regular expression syntax that both JS and .NET can execute. In this case, to make a regex ignore case it is necessary to use a character class construct like [a-zA-Z] to match both upper and lower case characters.

    If your validation is done on the server-side only, you can use the more powerful .NET regular expression syntax. In this case, you can place the (?i) option at the beginning of the regex to tell it to ignore case.

    So, if you want to use the out of the box validator, you're stuck with Geoff's solution of using character sets like this: [aA]

提交回复
热议问题