-2

Trying to use Regex to extract the repository name and pull request number from a url.

const paragraph = '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 = paragraph.match(regex);

console.log(found);
// expected output: Array ["awesome-repo", "12"]

But the response is just the entire url > Array ["https://github.com/johndoe/awesome-repo/pull/12"]

Any help would be appreciated

enter image description here

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313

2 Answers2

0

To only get the desired matching groups:

you could destructure to variables like:

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

console.log([name, pull]); // ["awesome-repo", "12"]

or without the matching groups:

const url = 'https://github.com/johndoe/awesome-repo/pull/12';
const result = url.match(/[^\/]+(?=\/pull\/)|\d+$/g);

console.log(result); // ["awesome-repo", "12"]

Your regex expressed the entire string (from https... to the last character in its entirety) when using the g (global) flag, consuming it and matching it. Therefore the first match will be the entire matching string. Remove the g (and the m multiline) to also collect the two matching groups () matches:

const paragraph = 'https://github.com/johndoe/awesome-repo/pull/12';
const regex = /https:\/\/github.com\/johndoe\/([a-zA-Z]+-[a-zA-Z]+)\/pull\/(\d+)/;
const found = paragraph.match(regex);

console.log(found); // ["https://github.com/johndoe/awesome-repo/pull/12", "awesome-repo", "12"]
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
-1

If using the global regex-flag (/g), the groups are not included. See this question. In your example you can simple omit both flags. Also, you can match the repo-name without defining the character-sets. Your regexp needs a dash - and cannot work on repos with numbers in it...

const paragraph = 'https://github.com/johndoe/awesome-repo/pull/12';
const regex = /https:\/\/github.com\/johndoe\/([^\/]+)\/pull\/(\d+)/;
const found = paragraph.match(regex);

console.log(found);
// expected output: Array ["awesome-repo", "12"]
// output: ["https://github.com/johndoe/awesome-repo/pull/12", "awesome-repo", "12", index: 0, input: "https://github.com/johndoe/awesome-repo/pull/12", groups: undefined]

boppy
  • 1,753
  • 12
  • 10