This is my regex:
这是我的正则表达式:
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;
};
It matches correctly if I enter a properly formatted value (eg. BALTEL0001) and an improper value (eg. BT001 or BALTEL0001BT0001). However, it also matches if I enter BALTEL0001BALTEL0002. I need it to match only if the value is one occurrence of the pattern. I'm sure it's something simple but I haven't hit on it.
如果我输入格式正确的值(例如BALTEL0001)和不正确的值(例如BT001或BALTEL0001BT0001),它会正确匹配。但是,如果我输入BALTEL0001BALTEL0002,它也会匹配。只有当值是模式的一次出现时我才需要它匹配。我确信它很简单,但我没有碰到它。
2 个解决方案
#1
2
You just need to add the start anchor. Your updated regex would look like this.
您只需添加起始锚点即可。您更新的正则表达式将如下所示。
/^[a-zA-Z]{6}[0-9]{4}$/
#2
4
here have a test : var re = /^[a-zA-Z]{6}[0-9]{4}$/;
这里有一个测试:var re = / ^ [a-zA-Z] {6} [0-9] {4} $ /;
#1
2
You just need to add the start anchor. Your updated regex would look like this.
您只需添加起始锚点即可。您更新的正则表达式将如下所示。
/^[a-zA-Z]{6}[0-9]{4}$/
#2
4
here have a test : var re = /^[a-zA-Z]{6}[0-9]{4}$/;
这里有一个测试:var re = / ^ [a-zA-Z] {6} [0-9] {4} $ /;