使用 JavaScript 验证正则表达式时出错 return

Wrong return when validating regex with JavaScript

有人可以看看我的正则表达式吗?我正在尝试验证一个 regex 组,但它匹配得太贪婪了。

/*Should match only 16 characters in total & must begin with BE and follow by 14 digits */ 
 
var re = /(?<iban>[/BE\B/(?={0-9})])/gm
     
let correctIban  = 'BE71096123456769'              // => should match
let badIbanOne   = 'BE13466123456767590kd'         // => should NOT match
let badIbanTwo   = 'BE13466123456767590679080176'  // => should NOT match
let badIbanThree = 'AZ71096123456769'              // => should NOT match

console.log(re.test(correctIban));   // => true
console.log(re.test(badIbanOne));    // => false
console.log(re.test(badIbanTwo));    // => false
console.log(re.test(badIbanThree));  // => false

编辑

感谢大家的帮助。这是 ES2018 中带有捕获组语法的代码,供那些想知道的人使用:(?<iban>^BE\d{14}$)

var re = /^BE\d{14}$/; 

解释:

  • ^ - 标记表达式的开始
  • BE - 文字字符'BE'
  • \d - 任何数字(与 [0-9] 相同)
  • {14} - 量词 - 精确 14
  • $ - 标记表达式结束

不需要所有额外的东西。

您可以在这里尝试:https://regex101.com/r/4wF3NG/1