开发者

How to write regex to match only one digit at end of pattern?

开发者 https://www.devze.com 2023-02-23 14:56 出处:网络
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?

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]$/;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号