GitHub Repository URL in JS
Match GitHub repository URLs and capture the owner and repo segments.
Try it in the JS tester →Pattern
regexJS
https?:\/\/(?:www\.)?github\.com\/([A-Za-z0-9](?:[A-Za-z0-9\-]{0,38})?)\/([A-Za-z0-9._\-]{1,100}) (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("https?:\\/\\/(?:www\\.)?github\\.com\\/([A-Za-z0-9](?:[A-Za-z0-9\\-]{0,38})?)\\/([A-Za-z0-9._\\-]{1,100})", "g");
const input = "Source at https://github.com/vercel/next.js or https://www.github.com/torvalds/linux";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
https?:\/\/(?:www\.)?github\.com\/ matches the domain with optional www and either http/https. The first capture ([A-Za-z0-9](?:[A-Za-z0-9\-]{0,38})?) follows GitHub's username rules: 1–39 chars, alphanumeric + hyphens, no leading/trailing hyphen (simplified). The second capture matches the repo name with the broader allowed character set.
Examples
Input
Source at https://github.com/vercel/next.js or https://www.github.com/torvalds/linuxMatches
https://github.com/vercel/next.jshttps://www.github.com/torvalds/linux
Input
Star: https://github.com/anthropics/claude-codeMatches
https://github.com/anthropics/claude-code
Input
no github linksNo match
—