jQuery 正则表达式问题

发布于 2024-09-11 11:43:31 字数 574 浏览 2 评论 0原文

我正在使用一个 Jquery 库,它采用文件中的 json 格式的正则表达式,并尝试从文件中验证它,

"Studentnumber":{
  regex: "/^[-/@#&$*\w\s]+$/",
  alertText: "* Invalid Student Number"
}, // ...

我可以在我的文本框字段中使用它,如下所示

<input type="text" class="validate[required,custom[Studentnumber]] Textbox">

早些时候,我曾经在该字段中使用 JavaScript 正则表达式,如下所示

<input type="text" validate="regex" regex='^[-/@#&$*\w\s]+$' name="sno" id="sno">

当我使用与 JavaScript 中的 jquery 相同的正则表达式。它与 JavaScript 中的验证不同,并且给出了不适当的结果。两者有什么区别?

Iam using one Jquery Library which takes the Regular expression in json format in on file and tries to validate that from file

"Studentnumber":{
  regex: "/^[-/@#&$*\w\s]+$/",
  alertText: "* Invalid Student Number"
}, // ...

I can use this in my text box field as follows

<input type="text" class="validate[required,custom[Studentnumber]] Textbox">

Earlier I used to have JavaScript Regular Expression for the field such as follows

<input type="text" validate="regex" regex='^[-/@#&$*\w\s]+

When I use the same regular expression that is used in the JavaScript for jquery. It is not validating as the same in JavaScript and giving inappropriate results. What is the Difference between the two?

name="sno" id="sno">

When I use the same regular expression that is used in the JavaScript for jquery. It is not validating as the same in JavaScript and giving inappropriate results. What is the Difference between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-09-18 11:43:31

由于您在字符串中使用退格键,并且之后想要它们,因此您需要转义它们,如下所示:

"Studentnumber":{
  regex: "/^[-/@#&$*\\w\\s]+$/",
  alertText: "* Invalid Student Number"
}

虽然,在查看此内容后我不能 100% 确定,它可能是 4 个反斜杠而不是 2 个,但是 < em>为什么此刻我无法理解。

Since your using backspaces in a string, and want them afterwards, you need to escape them, like this:

"Studentnumber":{
  regex: "/^[-/@#&$*\\w\\s]+$/",
  alertText: "* Invalid Student Number"
}

Though, I'm not 100% sure after looking at this, it might be 4 backslashes instead of 2, but the reasoning for why escapes me at the moment.

垂暮老矣 2024-09-18 11:43:31

这取决于插件如何与正则表达式配合使用,尝试删除引号或构造一个新实例:

"regex": /^[-/@#&$*\w\s]+$/

"regex": new RegExp("/^[-/@#&$*\w\s]+$/")

It depends on how the plugin works with regexp, try removing the quotes or construct a new instance:

"regex": /^[-/@#&$*\w\s]+$/

or

"regex": new RegExp("/^[-/@#&$*\w\s]+$/")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文