T
ToolPrime

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

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.

Related Tools