Dockerfile ENV Instruction in JS
Match Dockerfile `ENV KEY=value` (or `ENV KEY value`) instructions, capturing key and value.
Try it in the JS tester →Pattern
regexJS
^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$ (flags: gm)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^ENV\\s+([A-Z_][A-Z0-9_]*)(?:[=\\s]+)(.+)$", "gm");
const input = "ENV NODE_VERSION=20.10.0\\nENV PORT 3000";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
^ENV anchors to the line start (m flag enables per-line). \s+ requires whitespace. ([A-Z_][A-Z0-9_]*) captures the key (uppercase + underscores by convention). [=\s]+ matches either the modern `=` separator or the legacy whitespace separator. (.+)$ captures the rest of the line as the value.
Examples
Input
ENV NODE_VERSION=20.10.0\nENV PORT 3000Matches
ENV NODE_VERSION=20.10.0ENV PORT 3000
Input
ENV DATABASE_URL=postgres://localhost/dbMatches
ENV DATABASE_URL=postgres://localhost/db
Input
FROM node:20No match
—