针对特定容易猜测的模式的 RegEx 验证 [英] RegEx Validation for Specific Easily Guessed Patterns

查看:30
本文介绍了针对特定容易猜测的模式的 RegEx 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我完全不知道从哪里开始在我的 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 SaveContinue 按钮被点击后,单个输入被存储在一个数组中,然后joined 为一个数字.这是我必须验证的最终数字.

After s Save or Continue button is clicked, the individual inputs are stored in an array, and then joined 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 一次或多次,然后是 98,然后是行尾
    • \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 proceed
      • 9+[98]$ matches 9 one or more times, then either 9 or 8, then the end of the line
      • \d1{2}) matches any digit, followed by 1 twice

      由于负前瞻跟随行锚点的开始,我们也确保前瞻从那个位置开始,这就是为什么 \d1{2} 匹配 011, >111211、...、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} matches 011, 111, 211, ..., 911 and not 1211 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 提到将 999998 放在字符串中的任何位置应该使其无效:

      The OP mentioned that 999 and 998 placed anywhere in the string should invalidate it:

      查看此处使用的正则表达式

      ^(?!\d*9{2}[98]|\d1{2})\d{3,6}$
      

      与上面相同的正则表达式,除了负前瞻中的第一个选项.现在是 \d*9{2}[98],匹配字符串中任意位置的 999998(前面是任意数量的数字).

      Same regex as above except for the first option in the negative lookahead. It's now \d*9{2}[98], matching 999 or 998 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 just N11):

      查看此处使用的正则表达式

      ^(?!\d*9{2}[98]|[01]?\d1{2})\d{3,6}$
      

      与上面相同的正则表达式,除了负前瞻中的第二个选项.现在是 [01]?\d1{2},可选匹配 01,后跟任意数字,然后 11 (so 011, 111, 211, ..., 911, 0011, 0111, 0211, 0311, ..., 0911, 101111111211、...、1911).

      Same regex as above except for the second option in the negative lookahead. It's now [01]?\d1{2}, matching 0 or 1 optionally, followed by any digit, then 11 (so 011, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆