简体   繁体   中英

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.

How can I do that in ReactJs?

You may try using the pattern ^[az]{2}[0-9]{6}$ , for example:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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