Firefox提供SyntaxError:无效的regexp组 [英] Firefox gives SyntaxError: invalid regexp group
问题描述
我很少使用正则表达式来进行表单验证,并且我注意到我的项目无法通过firefox访问,因为它什么也没显示!但在控制台SyntaxError: invalid regexp group
I have few regular expression used for form validation and I noticed that my project is not accessible through firefox as it shows nothing! but give the error in the console, SyntaxError: invalid regexp group
nicRegex正在检查我所在国家的国民身份证.格式应为937962723V
或937962723X
或根据当前格式的任何11位数字.
nicRegex is checking for National Identity Card in my country. Format should be 937962723V
or 937962723X
or any 11 digit number according to the current format.
phoneRegex将使用我的国家/地区代码检查电话号码. 94121212121
或0762323232
phoneRegex is to check phone numbers with my country code. 94121212121
or 0762323232
const nicRegex = /^(?:19|20)?\d{2}(?:[01235678]\d\d(?<!(?:000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)))\d{4}(?:[vVxX])$/;
推荐答案
此处使用负向后搜索(Firefox当前不支持)来限制前三个数字.可以通过负前瞻同样很好地执行此限制,只是需要将其放置在3位数字模式之前
The negative lookbehind (not currently supported in Firefox) is used here to restrict the previous three digits. This restriction can be performed equally well with a negative lookahead, just it needs to be placed before the 3-digit pattern:
(?:[0-35-8]\d\d(?<!(?:000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)))
应该像
(?!000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)[0-35-8]\d\d
请注意,非捕获组在这里是多余的,我将其删除,然后[01235678]
= [0-35-8]
.
Note the non-capturing groups are redundant here, I removed them, and [01235678]
= [0-35-8]
.
最终的正则表达式看起来像
The final regex looks like
/^(?:19|20)?\d{2}(?!000|500|36[7-9]|3[7-9]\d|86[7-9]|8[7-9]\d)[0-35-8]\d\d\d{4}[vVxX]$/
这篇关于Firefox提供SyntaxError:无效的regexp组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!