Git Remote URL (HTTPS or SSH) in JS
Match git remote URLs in both `git@host:org/repo` and `https://host/org/repo` forms.
Try it in the JS tester →Pattern
regexJS
(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("(?:git@|https?:\\/\\/)([\\w.\\-]+)[:\\/]([\\w.\\-]+)\\/([\\w.\\-]+?)(?:\\.git)?\\/?$", "");
const input = "git@github.com:vercel/next.js.git";
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
(?:git@|https?:\/\/) matches either the SSH `git@` prefix or HTTP/HTTPS scheme. ([\w.\-]+) captures the host. [:\/] matches the host/path separator (colon for SSH, slash for HTTPS). The next two groups capture org and repo. (?:\.git)? optionally strips the trailing `.git`. \/? allows a trailing slash.
Examples
Input
git@github.com:vercel/next.js.gitMatches
git@github.com:vercel/next.js.git
Input
https://gitlab.com/group/subprojectMatches
https://gitlab.com/group/subproject
Input
not a git urlNo match
—