Regex for Environment Variable Name
Validates UPPER_SNAKE_CASE environment variable names starting with a letter.
Pattern
/^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/ Live Tester
Enter a string to test
Examples
✓ DATABASE_URL
✓ NODE_ENV
✓ AWS_ACCESS_KEY_ID
✗ database_url
✗ 123_VAR
✗ _LEADING_UNDERSCORE
Pattern Breakdown
^[A-Z] — starts with an uppercase letter
[A-Z0-9]* — followed by uppercase letters or digits
(_[A-Z0-9]+)*$ — optional underscore-separated segments
Code Snippets
Javascript
const regex = /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/;
regex.test("DATABASE_URL"); // true Python
import re
pattern = r"^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"
bool(re.match(pattern, "DATABASE_URL")) # True Php
$pattern = '/^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/';
preg_match($pattern, "DATABASE_URL"); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
Frequently Asked Questions
What is the regex for Environment Variable Name?▾
The regex pattern for Environment Variable Name is /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$/. Validates UPPER_SNAKE_CASE environment variable names starting with a letter.
How do I use the Environment Variable Name regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Environment Variable Name pattern match?▾
It matches strings like DATABASE_URL; it rejects strings like database_url. See the Examples section above for the full list of matching and non-matching cases.