【问题标题】:How do I make sure a regex matches one occurrence of a pattern如何确保正则表达式匹配模式的一次出现
【发布时间】:2013-03-20 12:57:42
【问题描述】:

这是我的正则表达式:

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,它也匹配。只有当值是模式的一次出现时,我才需要它匹配。我敢肯定这很简单,但我还没有想到。

【问题讨论】:

  • 试试这个表达式:/^([a-zA-Z]{6}[0-9]{4})$/

标签: regex xpages


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您只需要添加起始锚点。 您更新后的正则表达式将如下所示。

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

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 1970-01-01
      • 2021-07-09
      • 2022-08-13
      • 1970-01-01
      相关资源
      最近更新 更多