如何 javascript 匹配来自不同 url 的 7 个数字

How to javascript match exactly 7 numbers from different urls

我有 URL 个地址,例如

我的 javascript 小书签如下所示。

javascript:(function(){javascript:var location_pathname = document.location.href;var ggId = location_pathname.match(/^[0-9]{7}$/)[1]; window.open('http://localhost/script.php?id='+ggId, '_blank')})()

如何匹配每个网址中的这 7 个数字?以上是我的脚本,但它不起作用。如果我将示例与 "/\/something\/(.*)/" 代码匹配并打开新选项卡 http://localhost/script.php?id=9303033 但它仅在某些情况下有效。

删除正则表达式开头的 ^ 和末尾的 $,以匹配 url.

的任何位置
location_pathname.match(/([0-9]{7})/)[1];

如果斜线 (/) 和另一个斜线或 EOL 后恰好有 7 位数字,则正确的 RegEx 是

var re = /\/(\d{7})\/?|$/; //note (\d{7}) This is what will be captured
//tests
'http://www.gg.omg/whatever/4303013'.match(re)[1]; //4303013
'http://www.gg.omg/whatever/4303013/maybe'.match(re)[1]; //4303013
'http://www.gg.omg/whatever/430301'.match(re)[1]; //undefined