Home > other >  How to Create a Regular Expression in ReactJS
How to Create a Regular Expression in ReactJS

Time:12-09

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?

CodePudding user response:

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

var inputs = ["AB123456", "A123456", "AB12345"];
for (var i=0; i < inputs.length;   i) {
    if (/^[a-z]{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.

  • Related