Environment VariableRegex — Pattern, Examples & Code
^[A-Z_][A-Z0-9_]*$What it matches / doesn't match
| Matches ✅ | Does not match ❌ |
|---|---|
●DATABASE_URL | ●lowercase_key |
●NEXT_PUBLIC_API_KEY | ●1STARTS_WITH_DIGIT |
●_INTERNAL | ●HAS-HYPHEN |
How it works
Matches POSIX-compliant environment variable names: start with an uppercase letter or underscore, followed by any combination of uppercase letters, digits, and underscores. Lowercase letters are rejected since env var names are conventionally uppercase. This covers common conventions like DATABASE_URL, NEXT_PUBLIC_API_KEY, and __INTERNAL_VAR.
Code Usage
JavaScript
const regex = /^[A-Z_][A-Z0-9_]*$/;
regex.test("DATABASE_URL"); // truePython
import re
pattern = re.compile(r'^[A-Z_][A-Z0-9_]*$')
bool(pattern.match("DATABASE_URL")) # TrueGo
import "regexp"
r := regexp.MustCompile(`^[A-Z_][A-Z0-9_]*$`)
r.MatchString("DATABASE_URL") // trueNeed a custom regex for a different use case?
Generate a custom regex with AI →FAQ
What does the Environment Variable regex pattern match?
Matches POSIX-compliant environment variable names: start with an uppercase letter or underscore, followed by any combination of uppercase letters, digits, and underscores. Lowercase letters are rejected since env var names are conventionally uppercase. This covers common conventions like DATABASE_URL, NEXT_PUBLIC_API_KEY, and __INTERNAL_VAR.
How do I use the Environment Variable regex in JavaScript?
const regex = /^[A-Z_][A-Z0-9_]*$/; regex.test("DATABASE_URL"); // true
Can I use the Environment Variable regex in Python?
Yes. import re pattern = re.compile(r'^[A-Z_][A-Z0-9_]*$') bool(pattern.match("DATABASE_URL")) # True
