How to search a GitHub repo using regular expressions or case-sensitive matching

Today I learnt …

Here’s a little hack I’ve discovered. You’ll need to be logged in to GitHub for this to work, but it’s possible to search a repo using case-sensitive string matching — or even a regular expression — from GitHub’s website.

Here’s a hypothetical (and slightly contrived) example: let’s say you’re browsing through a repo online at https://github.com/example/foo. There’s a class called Bar, and somewhere in the repo an array of Bar is instantiated. You can’t remember if that array is called bar or bars. The Bar class is used throughout the code so there are a lot of results if you simply search for ‘bar’. You need case-sensitive regex search, but GitHub’s search doesn’t support that.

What you can do, though, is press . (aka full-stop or period) while https://github.com/example/foo is open in your browser. If you’re logged in this will actually open https://github.dev/example/foo — that is, github.dev not github.com — and set up Visual Studio Code in your browser. Once that’s ready you can open up VS Code’s search sidebar, turn on case-sensitive matching and regexes, and type bars? into the search box. And voilà, only code that matches bar or bars (in lowercase) will appear in the results.

Update, 20th September 2023

A few months after I posted this, in May 2023, GitHub updated their code search so you can search using a regex directly on github.com. Here’s the brief documentation. So there’s no need for this hack any more — using the example above you can simply search your repo for /bars?/.

One glitch in their current implementation is that regexes are only supported in the code search, so you’ll get zero matching results for issues, PRs, wiki pages, etc.