在剃刀中使用包含“@"的javascript正则表达式 [英] Using javascript regex containing '@' in razor
问题描述
我正在剃刀视图页面中使用 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.
我的正则表达式看起来像
my regex looks like
/^...otherpart @* ... other part$/
我试图添加一个 '@' 字符来使原始正则表达式中的 ...@@* ...,这消除了编译错误,但似乎使正则表达式停止工作.(我们在另一个不使用 razor 引擎的网络应用程序中使用了它,所以我知道它有效).
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?
推荐答案
你可以在它前面再添加一个@
来转义@@
,尝试省略量词<代码>*代码>.如果这似乎不起作用,则在函数周围添加
,它会告诉 Razor 不要解析内容.或者,您可以将 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.
如果由于某种原因您的字符串中有多个 @@
,请将代码块放在 @:@@
前
If for some reason you have multiple @@
in your string, place code blocks ahead @:@@
这篇关于在剃刀中使用包含“@"的javascript正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!