C#和Javascript中的正则表达式
我想为以下情况实施定期快递,有人能帮忙吗C#和Javascript中的正则表达式,javascript,c#,regex,asp.net-mvc,Javascript,C#,Regex,Asp.net Mvc,我想为以下情况实施定期快递,有人能帮忙吗 Numeric 0-9 with special character/ \ @ # $ % ^ & * ! - ~ . _ ( ) & space allowed like : 123abc not allowed like : 123#$%~. are allowed 你可以试试这个 ^[0-9!@#$%^&~*()_+\-=\[\]{};':"\\|,.<>\/?]*$ ^[0-9!@\\$%^&~*()\
Numeric 0-9 with special character/ \ @ # $ % ^ & * ! - ~ . _ ( ) & space allowed
like : 123abc not allowed
like : 123#$%~. are allowed
你可以试试这个
^[0-9!@#$%^&~*()_+\-=\[\]{};':"\\|,.<>\/?]*$
^[0-9!@\\$%^&~*()\+\-=\[\]{};':“\\\\\,.\/?]*$
对于正则表达式本身,您应该能够在方括号中列出允许的字符:
^[0-9/\\@#$%^&*!-~._()\ ]*$
在结尾处(尾随$之前)使用*匹配0个或多个字符(即如果空字符串可以),或在结尾处(尾随$之前)使用+匹配1个或多个字符
根据语言/regex实现的不同,您可能需要对模式字符串中的更多字符进行转义。您自己几乎已经编写了正则表达式,您需要将这些字符添加到a中,并使用a和表达式进行正确的转义
^[0-9/\\@#$%^&*!~._() -]+$
在C#中,您可以使用该方法验证:
if (Regex.IsMatch(input, @"^[0-9/\\@#$%^&*!~._() -]+$")) { ... }
var re = /^[0-9/\\@#$%^&*!~._() -]+$/
if (re.test(input)) { ... }
在JS中,您可以使用该方法验证:
if (Regex.IsMatch(input, @"^[0-9/\\@#$%^&*!~._() -]+$")) { ... }
var re = /^[0-9/\\@#$%^&*!~._() -]+$/
if (re.test(input)) { ... }
感谢Paul的快速回复。它采用字母值,也像a、b、c、d……我想禁用字母。好的,所以你想匹配完整的输入。我已经编辑了上面的答案,在开始处包括^,在结束处包括$。