C# 和 Javascript 中的正则表达式

Pra*_*hta 0 javascript c# regex asp.net-mvc

我想为以下条件实施正则快递,请任何人帮忙

Numeric 0-9 with special character/ \ @ # $ % ^ & * ! - ~ . _ ( ) & space allowed


like : 123abc not allowed
like : 123#$%~. are allowed
Run Code Online (Sandbox Code Playgroud)

hwn*_*wnd 5

您几乎自己编写了正则表达式,您需要将这些字符添加到具有适当转义的字符类中,使用量词锚定您的表达式。

^[0-9/\\@#$%^&*!~._() -]+$
Run Code Online (Sandbox Code Playgroud)

在 C# 中,您可以使用该Regex.IsMatch()方法进行验证:

if (Regex.IsMatch(input, @"^[0-9/\\@#$%^&*!~._() -]+$")) { ... }
Run Code Online (Sandbox Code Playgroud)

在JS中,您可以使用该test()方法进行验证:

var re = /^[0-9/\\@#$%^&*!~._() -]+$/
if (re.test(input)) { ... }
Run Code Online (Sandbox Code Playgroud)