Code
Environment variable assignment (.env line) Regex
Dotenv files and shell scripts set environment variables with lines like NAME=value. This pattern matches one such line: an optional export keyword, an uppercase name made of letters, digits and underscores that does not start with a digit, an equals sign with no spaces around it, and a value that is unquoted, single-quoted or double-quoted. A trailing comment after the value is allowed. It is useful for linting .env files, loading config in small scripts and catching typos in deployment settings. It does not support multi-line values or variable interpolation, and lowercase names are rejected by convention.
Pattern (JavaScript)
/^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|'[^']*'|[^\s#"']*)\s*(?:#.*)?$/Starts at the start of the line/string, then (the text "export", then one or more whitespace characters), optionally (zero or one time), then one of the characters A-Z_, then one of the characters A-Z0-9_, zero or more times, then the character "=", then (the character """, then any character except ", zero or more times, then the character """ OR the character "'", then any character except ', zero or more times, then the character "'" OR any character except \s#"', zero or more times), then a whitespace character (space, tab, newline), zero or more times, then (the character "#", then any character, zero or more times), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ API_URL=https://api.example.com
- ✓ export NODE_ENV=production
- ✓ DB_PASSWORD="not-a-real-secret"
- ✓ EMPTY_VALUE=
Does not match
- ✗ api_url=x
- ✗ MY VAR=1
- ✗ =value
- ✗ 2FA_KEY=abc
Try it
0 matches (JavaScript engine)
API_URL=https://api.example.com
export NODE_ENV=production
DB_PASSWORD="not-a-real-secret"
EMPTY_VALUE=
api_url=x
MY VAR=1
=value
2FA_KEY=abcCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|'[^']*'|[^\s#"']*)\s*(?:#.*)?$/g;
const text = "API_URL=https://api.example.com";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile('^(?:export\\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|\'[^\']*\'|[^\\s#"\']*)\\s*(?:#.*)?$')
text = 'API_URL=https://api.example.com'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|\'[^\']*\'|[^\s#"\']*)\s*(?:#.*)?$/';
$text = 'API_URL=https://api.example.com';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:export\\s+)?[A-Z_][A-Z0-9_]*=(?:\"[^\"]*\"|'[^']*'|[^\\s#\"']*)\\s*(?:#.*)?$");
String text = "API_URL=https://api.example.com";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|'[^']*'|[^\s#"']*)\s*(?:#.*)?$`)
text := "API_URL=https://api.example.com"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:""[^""]*""|'[^']*'|[^\s#""']*)\s*(?:#.*)?$");
var text = "API_URL=https://api.example.com";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r#"^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|'[^']*'|[^\s#"']*)\s*(?:#.*)?$"#).unwrap();
let text = "API_URL=https://api.example.com";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:export\s+)?[A-Z_][A-Z0-9_]*=(?:"[^"]*"|'[^']*'|[^\s#"']*)\s*(?:#.*)?$/
text = 'API_URL=https://api.example.com'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are lowercase names rejected?
- Environment variables are uppercase by strong convention, and many tools treat lowercase names as a mistake. Add a-z to both name character classes if your project uses them.
- Are spaces around the equals sign allowed?
- No. Shells treat NAME = value as a command named NAME, so most dotenv parsers also reject it. Requiring no spaces catches lines that would fail in a shell.
- How do I capture the name and value?
- Wrap the name part and the value alternation in capture groups. Strip surrounding quotes from the value afterwards, since the group includes them for quoted values.