Searching efficiently in VS Code can save you hours of manual work. While the default search is useful, regular expressions (regex) unlock powerful pattern-matching capabilities. In this guide, I'll break down essential regex patterns and show you how to use them in VS Code's search (Ctrl+F / Cmd+F).
Why Use Regex in VS Code?
- Find complex patterns (emails, variables, broken links).
- Refactor code faster (bulk rename, extract data).
- Search across files (
Ctrl+Shift+F / Cmd+Shift+F).
Essential Regex Patterns for VS Code
1. Basic Text Matching
hello → Matches exact word "hello".
h.llo → Matches "hallo", "hxllo" (. = any character).
2. Matching Multiple Characters
[aeiou] → Matches any vowel.
gr[ae]y → Finds "gray" or "grey".
[^aeiou] → Matches any character except vowels.
3. Quantifiers (How Many Times?)
a* → Matches 0 or more "a"s (e.g., "", "a", "aa").
a+ → Matches 1 or more "a"s (e.g., "a", "aa").
a{2,4} → Matches 2 to 4 "a"s (e.g., "aa", "aaa").
4. Word & Boundary Matching
\bword\b → Matches whole word "word" (not "keyword").
^Hello → Matches "Hello" at the start of a line.
end$ → Matches "end" at the end of a line.
5. Groups & Capturing
(abc)+ → Matches "abc", "abcabc".
(img|src)=["'](.*?)["'] → Finds HTML image sources.
6. Lazy vs. Greedy Matching
".*" → Greedy (matches entire "hello" and "world").
".*?" → Lazy (matches "hello", "world" separately).
7. Special Characters
\d → Any digit (0-9).
\s → Whitespace (space, tab).
\w → Word character (a-z, A-Z, 0-9, _).
How to Use Regex in VS Code
- Open search (
Ctrl+F / Cmd+F).
- Enable regex mode by clicking
.* (or press Alt+R).
- Enter your pattern (e.g.,
\b\d{3}-\d{3}-\d{4}\b for phone numbers).
- Press
Enter to see matches.
Pro Tip: Use Ctrl+Shift+F to search across files with regex!
Real-World Examples
✅ Find all email addresses:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
**Final Tips**
Test patterns at [regex101.com][1] before using them.
Bookmark this cheat sheet for quick reference!
[1]: https://regex101.com