在 ASP.NET RegularExpressionValidator 中使正则表达式不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2641236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make regular expression case insensitive in ASP.NET RegularExpressionValidator
提问by Jeremy
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, so I need to specify case insensitivity in the pattern.
鉴于此正则表达式:"^[0-9]*\s*(lbs|kg|kgs)$"如何使其不区分大小写?我试图在 .net 正则表达式验证器中使用它,所以我需要在模式中指定不区分大小写。
I can not use the RegexOptions programatically because I am specifying the regular expression in a RegularExpressionValidator
我无法以编程方式使用 RegexOptions,因为我在 RegularExpressionValidator 中指定了正则表达式
回答by Jeremy
I found out.
我发现。
Case sensitive: ^[0-9]\s(lbs|kg|kgs)$
区分大小写: ^[0-9]\s(lbs|kg|kgs)$
Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)
不区分大小写: (?i:^[0-9]\s(lbs|kg|kgs)$)
I believe that this is specific to the .NET implementation of regular expressions. So if you use this in the RegularExpressionValidator you have to turn off client side validation because the javascript regex parser will not recognize the ?itoken.
我相信这是特定于正则表达式的 .NET 实现的。因此,如果您在 RegularExpressionValidator 中使用它,您必须关闭客户端验证,因为 javascript regex 解析器将无法识别?i令牌。
回答by Richard JP Le Guen
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':
在其他语言中,您通常可以在 Reg Ex 结束后指定一个 RegEx 修饰符;“不区分大小写”修饰符是“i”:
In Perl:
在 Perl 中:
if($var =~ /^[0-9]\s(lbs|kg|kgs)$/i) { # the /i means case insensitive
# ...
}
In PHP:
在 PHP 中:
if(preg_match("/^[0-9]\s(lbs|kg|kgs)$/i", $var)) {
// ...
}
回答by goodeye
Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.
这是使用 CustomValidator 的替代方法,当真正需要不区分大小写的服务器端和客户端时;而上/下 [A-Za-z] 字符方法太多了。
This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.
这混合了各种其他答案,使用服务器端 RegEx 对象和客户端 JavaScript 语法。
CustomValidator:
自定义验证器:
<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />
Code behind:
后面的代码:
protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
Regex re = new Regex(@"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
args.IsValid = re.IsMatch(args.Value);
}
Client-side validation function:
客户端验证功能:
function cvWeight_Validate(sender, args) {
var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
args.IsValid = reWeight.test(args);
}
This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error *shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.
这对我来说很好用,除非使用 ValidationSummary。在客户端验证中,错误*显示,但我无法在摘要中显示错误消息。摘要仅在提交时显示。我认为它应该显示;我有更新面板和遗留代码的混合,这可能是问题。
回答by Geoff Reedy
Easiest here is to just modify the regex to
这里最简单的是将正则表达式修改为
^[0-9]*\s*([lL][bB][sS]|[kK][gG][sS]?)$
It's awful to read, but it will work fine.
读起来很糟糕,但它会工作得很好。
回答by KyleMit
Can we make Regex case-insensitive in C#? : Yes
我们可以在C#中使Regex 不区分大小写吗?:是的
Set option inline via
(?i)pattern constructor viaRegexOptions.IgnoreCaseparam
通过
(?i)模式构造或通过RegexOptions.IgnoreCase参数内联设置选项
Can we make Regex case-insensitive in JavaScript? : Yes
我们可以在JavaScript中使Regex 不区分大小写吗?:是的
Set flag via
/pattern/flagssyntax or the insensitive flag/REGEX/i
通过语法或不敏感标志设置标志
/pattern/flags/REGEX/i
(Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute?: No
(旁白)我们可以让正则表达式在HTML5 模式属性中不区分大小写吗?:没有
As Chris R. Timmons points out on RegularExpressionValidatordoesn't ignore case:
正如 Chris R. Timmons 所指出的,RegularExpressionValidator不要忽略大小写:
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.
RegularExpressionValidator 控件中没有允许设置正则表达式选项的属性。
如果您的控件同时执行客户端和服务器端验证,则正则表达式必须使用 JS 和 .NET 都可以执行的正则表达式语法的子集。在这种情况下,要使正则表达式忽略大小写,必须使用字符类构造,例如
[a-zA-Z]匹配大写和小写字符。如果您的验证仅在服务器端完成,您可以使用更强大的 .NET 正则表达式语法。在这种情况下,您可以将
(?i)选项放在正则表达式的开头以告诉它忽略大小写。
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]
所以,如果你想使用开箱即用的验证器,你会被 Geoff 使用这样的字符集的解决方案所困扰: [aA]

