我该如何确保正则表达式匹配模式的一个实例?

3
这是我的正则表达式:
var re = /[a-zA-Z]{6}[0-9]{4}$/;
if (storageTrayId == "") {
return false;
} else if(!re.test(storageTrayId)) {
alert('Storage Tray ID must be in the format \n ssstttnnnn (where sss is an alphabetic identifier for the Bulk File Center, ttt is an alphabetic identifer for the bundle type stored, and nnnn is the sequence number)');
 return false;
};

如果我输入正确格式的值(例如BALTEL0001)和不正确格式的值(例如BT001或BALTEL0001BT0001),它会正确匹配。但是,如果我输入BALTEL0001BALTEL0002,它也会匹配。我需要它只匹配模式出现一次的值。我相信这很简单,但我还没有想到方法。


1
尝试使用这个表达式:/^([a-zA-Z]{6}[0-9]{4})$/ - Valky
2个回答

4

这里有一个测试:var re = /^[a-zA-Z]{6}[0-9]{4}$/;


2

你只需要添加起始锚点。 你更新后的正则表达式应该是这样的。

/^[a-zA-Z]{6}[0-9]{4}$/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接