Javascript 从GitHub url获取回购名称和拉取编号

Javascript 从GitHub url获取回购名称和拉取编号,javascript,regex,Javascript,Regex,尝试使用正则表达式从url提取存储库名称和请求号 const段落class='1〕https://github.com/johndoe/awesome-repo/pull/12'; const regex=/https:\/\/github.com\/johndoe\/([a-zA-Z]+-[a-zA-Z]+)\/pull\/(\d+)/gm; const found=段落匹配(regex); console.log(找到); //预期输出:数组[“awesome repo”,“12”]如果使

尝试使用正则表达式从url提取存储库名称和请求号

const段落class='1〕https://github.com/johndoe/awesome-repo/pull/12';
const regex=/https:\/\/github.com\/johndoe\/([a-zA-Z]+-[a-zA-Z]+)\/pull\/(\d+)/gm;
const found=段落匹配(regex);
console.log(找到);

//预期输出:数组[“awesome repo”,“12”]
如果使用全局正则表达式标志(
/g
),则不包括组。看见在您的示例中,您可以简单地忽略这两个标志。此外,您还可以在不定义字符集的情况下匹配repo名称。您的regexp需要一个破折号
-
,无法处理包含数字的回购

const段落class='1〕https://github.com/johndoe/awesome-repo/pull/12';
const regex=/https:\/\/github.com\/johndoe\/([^\/]+)\/pull\/(\d+)/;
const found=段落匹配(regex);
console.log(找到);
//预期输出:数组[“绝妙回购”,“12”]
//输出:[”https://github.com/johndoe/awesome-repo/pull/12,“绝妙回购”,“12”,索引:0,输入:https://github.com/johndoe/awesome-repo/pull/12“,组:未定义]

仅获取所需的匹配组:

您可以将结构分解为以下变量:

consturl='1〕https://github.com/johndoe/awesome-repo/pull/12';
const[all,name,pull]=url.match(/([^\/]+)\/pull\/(\d+)$/);

console.log([name,pull]);//[“awesome repo”,“12”]
尝试忽略
/gm
标志,然后匹配项位于第1组和第2组,如
控制台.log(找到[1])