我正在尝试验证字符串不应以某些字符开头且不应包含 <>。
[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression(@"(?=(^(?!.*[><]).*$))(?=(^(?![@\+\-=\*]).*))", ErrorMessage = "firstname"+ Constants.DisplayMessage)]
public string firstname { get; set; }
这个正则表达式在 JavaScript 中工作。但它在 c# 中不起作用。我已经花了将近 1 个小时但没有结果请帮助我。
你也尝试过使用 (^(?!.*[><]).*$)|(^(?![@\+\-=\*]).*)
这个正则表达式。但它不起作用。
我不擅长正则表达式,所以请帮助我。
最佳答案
根据您的描述,正则表达式需要做什么,以下模式应该有效:
^(?![@=*-])(?!.*[<>]).*$
说明:
^
(?![@=*-]) from the start of string, assert that @=*- is not the first character
(?!.*[<>]) also assert that <> does not appear anywhere in the string
.* then match anything (our assertions having be proven true)
$
Demo
此模式也适用于 C#,您可以通过探索下面的第二个演示看到这一点。
C# Demo
关于javascript - 正则表达式不能在 c# 中工作,但可以在 javascript 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48335036/