I'm limiting the user to input a string contains four letters following with 4 digit of numbers, like the format qwer1234 in vue.js. I have tried the regex b[a-z]{4} [0-9]{4}\b
but it seems not working. Are there any mistake I make?
CodePudding user response:
I deleted the ' ', and it works!
var pattern = /\b[a-z]{4}[0-9]{4}\b/;
var str = "";
console.log(pattern.test(str));
CodePudding user response:
4 lowercase characters would be:
[a-z]{4}
4 digits would be
\d{4}
a word boundary is
\b
the start or end of a line are marked like this:
^...$
So, if this phrase should be contained (possibly only as a part) of a bigger phrase, put word boundaries around it.
\b[a-z]{4}\d{4}\b
or if the entire phrase has to match exactly, then you can put it between start and end characters.
^[a-z]{4}\d{4}$