jquery - if(this.id.match(/^None_+\d+_+\d+$/)) 中的错误是什么

标签 jquery regex

嗨,我编写了一个正则表达式来匹配模式,例如

"None_123_234"  

下面是我的代码

if(this.id.match(/^None_+\d+_+\d+$/))  

但是它不起作用。我们可以用其他方式写吗?请有人帮助我。

最佳答案

它不起作用,因为它不会匹配:

"None_123_234"

这是因为插入符^,它将断言匹配是从字符串的开头完成的。在本例中,字符串的开头是双引号 "。此外,美元符号 $ 将断言该模式将持续到字符串末尾。本例中的字符串又是双引号 ".

这是您给出的正则表达式 (/^None_+\d+_+\d+$/) 的确切解释:

  1. Assert position at the beginning of the string «^»
  2. Match the characters “None” literally «None»
  3. Match the character “_” literally «_+»

    a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

  4. Match a single digit 0..9 «\d+»

    a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

  5. Match the character “_” literally «_+»

    a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

  6. Match a single digit 0..9 «\d+»

    a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

  7. Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

您有两个选择:

  1. 您的字符串不应以 None_ 以外的任何内容开头...
  2. 或者,您应该更改正则表达式以匹配文本中任意位置的模式,如下所示:
if(this.id.match(/None_+\d+_+\d+/))

关于jquery - if(this.id.match(/^None_+\d+_+\d+$/)) 中的错误是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492507/

相关文章:

jquery - 仅用一个导航器控制两个光滑的 slider

javascript - 需要用Javascript替换 "$"为 "_"

regex - sed:删除两个模式之间的字符串,使第二个模式保持不变(包括一半)

javascript - 正则表达式转义 HTML 标签

Javascript 正则表达式替换不在 html 属性中的文本

asp.net - 从 JSON 对象中提取字符串值

javascript - 在部分 View 中动态加载后如何使 JQuery DatePicker 工作?

javascript - 不在桌面版时隐藏 Logo

javascript - Jquery获取列表元素的id并根据点击调用函数

SQL 查询进行反向 CONTAINS 搜索?