针对特定容易猜测的模式的 RegEx 验证 [英] RegEx Validation for Specific Easily Guessed Patterns
问题描述
所以我完全不知道从哪里开始在我的 React 应用程序中创建 RegEx 验证模式.
So I am completely not knowing in where to start in creating a RegEx validation pattern in my React application.
我有各种输入框,其中(取决于某些条件)会有 3、4、5 或 6 位密码(同样数量的输入框呈现在屏幕上,每个接受 1 个数字).
I have various input boxes, of which (depending on certain criteria) there will be either a 3, 4, 5, or 6 digit passcode (and that same number of input boxes rendered on screen to accept 1 number each).
s Save
或 Continue
按钮被点击后,单个输入被存储在一个数组中,然后join
ed 为一个数字.这是我必须验证的最终数字.
After s Save
or Continue
button is clicked, the individual inputs are stored in an array, and then join
ed to be one number. It is this final number that I must validate.
我需要做的是编写一个验证方法 - 在 React 中 - 它执行以下验证:
What I need to do is write a validation method - in React - which performs the following validations:
对于 3 位数字:
不能是 999、998 或紧跟在第一个数字之后的 11(911、611 等)
Cannot be 999, 998, or include 11 immediately after the first digit (911, 611, etc.)
对于 4 位数字:
不能是 9999、9998 或紧跟在第一个数字之后的 11(8112、5112 等)
Cannot be 9999, 9998, or include 11 immediately after the first digit (8112, 5112, etc.)
对于 5 位数字:
不能是 99999、99998 或紧跟在第一个数字之后的 11(71122、41122 等)
Cannot be 99999, 99998, or include 11 immediately after the first digit (71122, 41122, etc.)
对于 6 位数字:
不能是 999999、999998 或紧跟在第一个数字之后的 11(611222、311222 等)
Cannot be 999999, 999998, or include 11 immediately after the first digit (611222, 311222, etc.)
这样做让我很痛苦,但我确实缺乏 RegEx 空间,并且不确定如何实施这样的事情.
It pains me to do so, but I am really lacking in the RegEx space, and am uncertain how to go about implementing something like this.
还应该注意的是,我没有使用 Redux 或随之而来的验证.必须使用接受参数(从输入框检索到的最终数字)并针对该参数运行验证(如果失败则显示错误消息em>)
It should also be noted that I am not using Redux or the validations that come along with it. Gotta attack this one with a util method that accepts an argument (the final number retrieved from the input boxes) and runs the validation against that (displaying an error message if there is a failure)
有没有人可以提供一些有关如何开始使用 RegEx 的见解...?
Is there anyone that can provide some insight on how to go about getting started for the RegEx...?
提前致谢!
推荐答案
正如我在您的问题下方的评论,您可以使用以下正则表达式:
As in my comment below your question, you can use the following regex:
^(?!9+[98]$|\d1{2})\d{3,6}$
工作原理:
^
断言行首位置(?!9+[98]$|\d1{2})
否定前瞻确保以下任一选项不会继续9+[98]$
匹配9
一次或多次,然后是9
或8
,然后是行尾\d1{2})
匹配任何数字,后跟1
两次
^
assert position at the start of the line(?!9+[98]$|\d1{2})
negative lookahead ensuring either of the the following options does not proceed9+[98]$
matches9
one or more times, then either9
or8
, then the end of the line\d1{2})
matches any digit, followed by1
twice
由于负前瞻跟随行锚点的开始,我们也确保前瞻从那个位置开始,这就是为什么
\d1{2}
匹配011
,>111
、211
、...、911
而不是1211
或其他.Since the negative lookahead follows the start of line anchor, we also ensure the lookahead starts at that position, that's why
\d1{2}
matches011
,111
,211
, ...,911
and not1211
or others.代码如下:
s = ['999','998','911','611','9999','9998','8112','5112','99999','99998','71122','41122','999999','999998','611222','311222','123','6211','99989','121212'] r = /^(?!9+[98]$|\d1{2})\d{3,6}$/ for (x of s) { console.log(x.match(r) ? x + ': true' : x + ': false') }
--
OP 提到将
999
和998
放在字符串中的任何位置应该使其无效:The OP mentioned that
999
and998
placed anywhere in the string should invalidate it:^(?!\d*9{2}[98]|\d1{2})\d{3,6}$
与上面相同的正则表达式,除了负前瞻中的第一个选项.现在是
\d*9{2}[98]
,匹配字符串中任意位置的999
或998
(前面是任意数量的数字).Same regex as above except for the first option in the negative lookahead. It's now
\d*9{2}[98]
, matching999
or998
anywhere in the string (preceded by any number of digits).s = ['999','998','911','611','9999','9998','8112','5112','99999','99998','71122','41122','999999','999998','611222','311222','123','6211','99989','121212'] r = /^(?!\d*9{2}[98]|\d1{2})\d{3,6}$/ for (x of s) { console.log(x.match(r) ? x + ': true' : x + ': false') }
--
OP 提到
0N11
的格式应该失效(不仅仅是N11
):The OP mentioned that the format of
0N11
should be invalidated (not justN11
):^(?!\d*9{2}[98]|[01]?\d1{2})\d{3,6}$
与上面相同的正则表达式,除了负前瞻中的第二个选项.现在是
[01]?\d1{2}
,可选匹配0
或1
,后跟任意数字,然后11
(so011
,111
,211
, ...,911
,0011
,0111
,0211
,0311
, ...,0911
,1011
、1111
、1211
、...、1911
).Same regex as above except for the second option in the negative lookahead. It's now
[01]?\d1{2}
, matching0
or1
optionally, followed by any digit, then11
(so011
,111
,211
, ...,911
,0011
,0111
,0211
,0311
, ...,0911
,1011
,1111
,1211
, ...,1911
).s = ['999','998','911','611','9999','9998','8112','5112','99999','99998','71122','41122','999999','999998','611222','311222','123','6211','99989','121212'] r = /^(?!\d*9{2}[98]|[01]?\d1{2})\d{3,6}$/ for (x of s) { console.log(x.match(r) ? x + ': true' : x + ': false') }
这篇关于针对特定容易猜测的模式的 RegEx 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!