Regex To Match Git Repositories

If you’re using Git for version control, you may want to know how to match Git repository URLs.

My problem was when I wanted to create a regex for this purpose. It took me hours before I found what I believe to be the best regex code for tasks like these. With this tutorial, hopefully, you’ll save yourself some time and make your job easier.

/((git|ssh|http(s)?)|([email protected][\w\.]+))(:(//)?)([\w\[email protected]\:/\-~]+)(\.git)(/)?/g

Matches:

Non-matches:

See Also:

Regex Is Copied!