Regex只匹配长度正好为5位数的数字-Javascript



在javascript中-我只想提取长度正好为5位数的数字。

let example = "The quick brown 22 333 44 55555

在这种情况下,我希望它匹配并给我和55555

编辑:我想明白了。由于我想要精确的五位数字:.match(/(?<!d)d{5}(?!d)/g)

这确保了它正好是五个,并且没有其他数字超过它

这样就可以了。

(?<!d)d{5}(?!d)

演示

您要查找的正则表达式是:

/d{5}/
  • 数字的d
  • {5}5次

一个例子:

const example = "hello c12345 df444 3444, 55555";
const matches = example.match(/d{5}/g);
console.log(matches);
// => [ '12345', '55555' ]
"[0-9]{5}"
[0-9] // match any numbers between 0 to 9
{5} // match exactly 5 times

最新更新