简体   繁体   English

React.js RegExp 总是返回 false

[英]React.js RegExp always returns false

I have this code to check if my fields are all good to go.我有这段代码来检查我的字段是否都适合 go。 I have my back-end up and working, but I'm struggling to check the regex inside my react method.我有我的后端并正在工作,但我正在努力检查我的反应方法中的正则表达式。 All I did was created a regex in regex101 before used it on the input pattern, but I wanted to change it to the method.我所做的只是在 regex101 中创建了一个正则表达式,然后在输入模式上使用它,但我想将其更改为方法。 So basically the regex always returns false...所以基本上正则表达式总是返回假......

// Then check regex
const regInput = new RegExp(
  '^(?:(?:IT|SM)d{2}[A-Z]d{22}|CYd{2}[A-Z]d{23}|NLd{2}[A-Z]{4}d{10}|LVd{2}[A-Z]{4}d{13}|(?:BG|BH|GB|IE)d{2}[A-Z]{4}d{14}|GId{2}[A-Z]{4}d{15}|ROd{2}[A-Z]{4}d{16}|KWd{2}[A-Z]{4}d{22}|MTd{2}[A-Z]{4}d{23}|NOd{13}|(?:DK|FI|GL|FO)d{16}|MKd{17}|(?:AT|EE|KZ|LU|XK)d{18}|(?:BA|HR|LI|CH|CR)d{19}|(?:GE|DE|LT|ME|RS)d{18}|ILd{21}|(?:AD|CZ|ES|MD|SA)d{22}|PTd{23}|(?:BE|IS)d{24}|(?:FR|MR|MC)d{25}|(?:AL|DO|LB|PL)d{26}|(?:AZ|HU)d{27}|(?:GR|MU)d{28})$'
);

if (!regInput.test(this.state.iban)) {
  this.setState({
    error: true,
    errorMsg:
      'Sąskaitsssos numeris įvestas klaidingai, bandykite dar kartą',
  });
  console.log('error');
  return;
} else {
  console.log('LT597300010145601329');
}

在此处输入图像描述

Seems like your regex is the problem.似乎您的正则表达式是问题所在。 If you are going to have the pattern 2 letters followed by 18 digits, you can try this regex:如果您要使用 2 个字母后跟 18 个数字的模式,您可以试试这个正则表达式:

^[\w]{2}[\d]{18}$

Here is the regex for an input like: LT597300010145601329这是输入的regex ,例如: LT597300010145601329

Try:尝试:

[a-zA-Z]{2}\d{18}

Note: If you want only uppercase letters, then: [AZ]{2}\d{18}注意:如果只需要大写字母,则: [AZ]{2}\d{18}

According to this link here ,根据这里的链接

An IBAN, or international bank account number starts with a two-digit country code, then two numbers, followed by several more alphanumeric characters. IBAN 或国际银行帐号以两位国家代码开头,然后是两个数字,然后是几个字母数字字符。

You can use the following regex:您可以使用以下正则表达式:

^[a-zA-Z]{2}[\d]{2}[a-zA-Z0-9]{14,20}$

In short, this accepts a value with 2 letters(irrespective of the case) followed by 2 digits followed by alphanumeric characters ranging from 14-20 characters (You can change the length constraint if you have more details on the pattern).简而言之,这接受一个包含 2 个字母(不考虑大小写)的值,后跟 2 个数字,后跟 14-20 个字符的字母数字字符(如果您对模式有更多详细信息,可以更改长度限制)。


Detailed explaination:详细解释:

Match a single character present in the list below [a-zA-Z]匹配以下列表中的单个字符 [a-zA-Z]

  • {2} matches the previous token exactly 2 times {2} 与前一个标记完全匹配 2 次
  • az matches a single character in the range between a (index 97) and z (index 122) (case sensitive) az 匹配 a(索引 97)和 z(索引 122)之间范围内的单个字符(区分大小写)
  • AZ matches a single character in the range between A (index 65) and Z (index 90) (case sensitive) AZ 匹配 A(索引 65)和 Z(索引 90)之间范围内的单个字符(区分大小写)

Match a single character present in the list below [\d]匹配下面列表中存在的单个字符 [\d]

  • {2} matches the previous token exactly 2 times {2} 与前一个标记完全匹配 2 次

  • \d matches a digit (equivalent to [0-9]) \d 匹配一个数字(相当于 [0-9])

Match a single character present in the list below [a-zA-Z0-9]匹配以下列表中的单个字符 [a-zA-Z0-9]

  • {14,20} matches the previous token between 14 and 20 times, as many times as possible, giving back as needed (greedy) {14,20} 匹配前一个令牌 14 到 20 次,尽可能多次,根据需要回馈(贪婪)

  • $ asserts position at the end of a line $ 在行尾断言 position

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM