Git Remote URL (HTTPS or SSH)
Match git remote URLs in both `git@host:org/repo` and `https://host/org/repo` forms.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
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).
Python (re) code
import re
pattern = re.compile(r"(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$")
input_text = "git@github.com:vercel/next.js.git"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$`)
input := `git@github.com:vercel/next.js.git`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
(?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$Raw source: (?:git@|https?:\/\/)([\w.\-]+)[:\/]([\w.\-]+)\/([\w.\-]+?)(?:\.git)?\/?$
How it works
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
—Common use cases
- •.gitmodules / submodule resolution
- •Migration scripts between hosts
- •CI tooling that ingests repo URLs
- •Dependency-source resolution (Go modules, npm git deps)
Related patterns
Git Commit SHA
IdentifiersMatch Git commit hashes, both short (7 chars) and full (40 chars) forms.
npm Package Name
IdentifiersValidate npm package names including scoped packages (@org/package), per the npm naming spec.
AWS ARN (Amazon Resource Name)
IdentifiersMatch AWS ARNs (Amazon Resource Names) across commercial, China, and GovCloud partitions.
AWS S3 Bucket Name
IdentifiersValidate AWS S3 bucket names per the standard naming rules: 3–63 chars, lowercase, alphanumeric + dots + hyphens.