简体   繁体   English

如何在 ReactJS 中创建正则表达式

[英]How to Create a Regular Expression in ReactJS

I want to create a regular expression that starts with two letters and is followed by 6 digits only and compare it with a string useState that I have.我想创建一个以两个字母开头后跟 6 个数字的正则表达式,并将其与我拥有的字符串 useState 进行比较。

How can I do that in ReactJs?我怎么能在 ReactJs 中做到这一点?

You may try using the pattern ^[az]{2}[0-9]{6}$ , for example:您可以尝试使用模式^[az]{2}[0-9]{6}$ ,例如:

 var inputs = ["AB123456", "A123456", "AB12345"]; for (var i=0; i < inputs.length; ++i) { if (/^[az]{2}[0-9]{6}$/i.test(inputs[i])) { console.log("MATCH => " + inputs[i]); } else { console.log("NO MATCH => " + inputs[i]); } }

Note that I am running the above regex using the case insensitive /i flag.请注意,我正在使用不区分大小写的/i标志运行上述正则表达式。

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

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