正则表达式\ w不起作用 [英] Regular Expression \w is not working
本文介绍了正则表达式\ w不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在工作一个asp.net核心应用程序.
I am working an an asp.net core app.
我已经进行验证了.
我试图禁止用户仅输入空格.所以我用这个:
I am trying to prohibit the user from entering only spaces. So I use this:
[Display(Name = "Reason for Cancellation")]
[Required]
[RegularExpression(@"\w", ErrorMessage = CancellationValidationErrorMessage)]
[StringLength(245)] // 245 characters to allow for History Type prefix to be added
public string CancelJustificationComments { get; set; }
虽然错误弹出,指出我必须提供一个条目,但在其他任何输入上也都会出现错误.
And whilst the error pops up stating that i must supply an entry I also get the error on any other input as well.
怎么了?
推荐答案
正则表达式验证器要求整个文本与正则表达式匹配.因此,该\ w仅匹配单个字符条目.
The regex validator requires that the whole text matches the regex. So this \w only matches a single character entry.
解决方案:在其周围添加一些通配符:.* \ w.*
Solution: add some wildcards around it: .*\w.*
这篇关于正则表达式\ w不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文