Skip to main content
Code & Markup · In-Browser Regular Expression Lab

.env Variable Regex — Parse Environment Configuration Files

Parses standard `.env` file entries matching uppercase snake_case keys and their string values.

Flags:
//m
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:DATABASE_URL=postgres://localhost:5432/db

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

DATABASE_URL=postgres://localhost:5432/db Pass
NEXT_PUBLIC_API_KEY=xyz123 Pass
PORT=3000 Pass

Expected Invalid Samples (Must Reject)

123_KEY=val Rejected (Pass)
=no_key Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^([A-Z_][A-Z0-9_]*)=(.*)$/m;
const text = "DATABASE_URL=postgres://localhost:5432/db";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Matches lines in dotenv files, splitting key name and value separated by an equals sign. Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
([A-Z_][A-Z0-9_]*)Variable KeyStandard POSIX environment variable identifier.
=AssignmentEquals operator.
(.*)$ValueRaw configuration value.

Frequently Asked Questions

How to strip comments in .env files?
Lines starting with `#` should be filtered out with `/^\s*#/`.
Browse All 40 Patterns