How to write regex to match only one digit at end of pattern?
My field is supposed to be开发者_开发知识库 in the format of A111-1A1, but my regex allows the very last number to be more than one digit (eg. A111-1A1212341). How do I fix this?
Below is the regex I am currently using.
var validchar = /^[A-Z](([0-9]{3})+\-)[0-9][A-Z][0-9]+$/;
Remove the +
at the end of your pattern. That is what allows for more than one numeric at the end.
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
However, your pattern otherwise doesn't look right to do what you say you want. Is that really the exact pattern you are using?
Try this
var validchar = /^[A-Z][0-9]{3}\-[0-9][A-Z][0-9]$/;
Or remove the + from the end of your regex
var validchar = /^A-Z[0-9][A-Z][0-9]$/;
Just remove the final +
from your regex:
var validchar = /^[A-Z]([0-9]{3})+\-[0-9][A-Z][0-9]$/;
精彩评论