嗨,我编写了一个正则表达式来匹配模式,例如
"None_123_234"
下面是我的代码
if(this.id.match(/^None_+\d+_+\d+$/))
但是它不起作用。我们可以用其他方式写吗?请有人帮助我。
最佳答案
它不起作用,因为它不会匹配:
"None_123_234"
这是因为插入符^
,它将断言匹配是从字符串的开头完成的。在本例中,字符串的开头是双引号 "
。此外,美元符号 $
将断言该模式将持续到字符串末尾。本例中的字符串又是双引号 "
.
这是您给出的正则表达式 (/^None_+\d+_+\d+$/
) 的确切解释:
- Assert position at the beginning of the string «^»
- Match the characters “None” literally «None»
Match the character “_” literally «_+»
a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single digit 0..9 «\d+»
a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “_” literally «_+»
a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single digit 0..9 «\d+»
a. Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
- Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
您有两个选择:
- 您的字符串不应以
None_
以外的任何内容开头... - 或者,您应该更改正则表达式以匹配文本中任意位置的模式,如下所示:
if(this.id.match(/None_+\d+_+\d+/))
关于jquery - if(this.id.match(/^None_+\d+_+\d+$/)) 中的错误是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492507/