在剃须刀使用包含JavaScript正则表达式“@” [英] Using javascript regex containing '@' in razor

查看:110
本文介绍了在剃须刀使用包含JavaScript正则表达式“@”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在剃刀视图页面的JavaScript执行的电子邮件地址验证。
我将使用正则表达式类似于在在JavaScript中验证电子邮件地址的人?

I am performing email address validation with javascript in a razor view page. The regex I am going to use is similar to the one proposed at Validate email address in JavaScript?

然而,由于正则表达式中包含@字符,我得到解析错误时尝试运行Web应用程序。

However, because the regex contains an '@' character, I am getting parser error when try to run the web app.

我正则表达式看起来像

/^...otherpart @* ... other part$/

我想加一个@字符,使在源正则表达式... @@ * ......,这消除了编译错误,但似乎让正则表达式停止工作。 (我们在不使用剃刀引擎另一个Web应用程序使用过,所以我知道它的工作原理)。

I tried to add an '@' character to make the in the origin regex ... @@* ..., this eliminated the compilation error but seems to make the regex stops working. (we have used it in another web app that does not use razor engine, so I know it works).

是否有任何其他的方式来逃避@字符?

Is there any other way to escape the '@' character?

推荐答案

您可以添加其他 @ 在它的前面逃跑 @@ ,尽量留出quantifer * 。如果这似乎并没有工作,然后添加<文本>< /文本> 周围的功能,它告诉剃刀无法解析的内容。或者你可以把JavaScript为单独的文件来实现您的需求。

You can add another @ in front of it to escape @@, try leaving out the quantifer *. If this doesn't seem to work then add <text></text> around the function, it tells Razor to not parse the contents. Alternatively you can put Javascript in a separate file to accomplish your needs.

如果由于某种原因,你有多个 @@ 在你的字符串,地方code块提前 @:@@

If for some reason you have multiple @@ in your string, place code blocks ahead @:@@

这篇关于在剃须刀使用包含JavaScript正则表达式“@”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆