【发布时间】:2019-03-15 13:58:31
【问题描述】:
有人可以看看我的正则表达式吗?我正在尝试验证 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[0-9]{14}$/ -
为什么
在你的 reg exp 中?? -
@WiktorStribiżew 的正则表达式会做你想做的事——为什么你的正则表达式中有所有这些额外的东西,比如
<iban>?如果这与您要解决的问题有关,请说明。 -
@AndersonPimentel 在 JS 中,不管用什么。在这里,复制/粘贴。
-
@Pointy JS ECMAScrpt 2018 supports named groups.
标签: javascript regex regex-group