Skip to content

Regex & Glob Patterns (80/20 Guide)#

Note: This guide covers both Glob patterns (used in .gitignore, file paths) and Regular Expressions (used in VS Code search, grep, code validation).


1️⃣ Glob Patterns (For Files & Git)#

Used in .gitignore, terminal commands, and file explorers.

Pattern Meaning Example Matches
* Matches any characters (except /) *.py script.py, test.py
? Matches exactly one character app?.js app1.js, appA.js
** Matches directories recursively logs/**/*.log logs/error.log, logs/2024/jan/info.log
! Negate (Don't ignore) !important.log Keeps important.log even if *.log is ignored
[] Matches any one character inside file[0-9].txt file1.txt, file5.txt
/ at start Matches from project root only /build build/ folder at root (not src/build)
/ at end Matches only directories temp/ temp folder (not a file named temp)

💡 Common .gitignore Examples#

# Ignore all .log files anywhere
*.log

# Ignore node_modules anywhere
node_modules/

# Ignore secrets.js only at root
/secrets.js

# Ignore everything in 'build' folder
build/

# But keep 'build/index.html'
!build/index.html

# Ignore all .map files in any 'dist' folder
dist/**/*.map

2️⃣ Regular Expressions (For Search & Code)#

Used in VS Code Find/Replace (Ctrl+F -> Alt+R), grep, and validation logic.

🔹 The Essentials (80% of usage)#

Symbol Name Meaning Example Matches
. Dot Any single character c.t cat, cut, c@t
* Star 0 or more of previous a*b b, ab, aaab
+ Plus 1 or more of previous a+b ab, aaab (not b)
? Question 0 or 1 of previous (optional) colou?r color, colour
\d Digit Any number (0-9) user_\d+ user_1, user_99
\w Word Letter, number, or _ \w+ varName, id_1
\s Whitespace Space, tab, newline import\s+ import
^ Start Start of line/string ^Error Line starting with "Error"
$ End End of line/string js$ Line ending with "js"
[] Set Any char inside [aeiou] Any vowel
() Group Group for capture/order (gr|br)ay gray, bray

🔹 VS Code Search Tricks#

Goal Regex Pattern Explanation
Find all console logs console\.log\(.*\) Matches console.log(...) with anything inside
Find TODOs //\s*TODO:.* Matches comments like // TODO: fix this
Find specific imports import.*from 'react' Finds React imports
Find empty lines ^\s*$ Matches lines with nothing or just spaces
Find hex colors #[0-9a-fA-F]{3,6} Matches #fff, #000000

3️⃣ Cheat Sheet Summary#

Task Use Pattern Example
Ignore folder in Git Glob node_modules/
Ignore all .env files Glob **/.env
Find email in text Regex \w+@\w+\.\w+
Find function defs Regex function\s+\w+\(
Match file extension Glob *.tsx