Kubernetes Label (key=value) in JS
Validate Kubernetes label `key=value` pairs per the K8s naming spec.
Try it in the JS tester →Pattern
regexJS
^([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)?$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^([a-zA-Z0-9](?:[\\w.\\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\\w.\\-]{0,61}[a-zA-Z0-9])?)?$", "");
const input = "app=myapp";
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
Both key and value follow the same rule: 1–63 chars, must start AND end with alphanumeric, can contain underscores/dots/hyphens in the middle. The value group is optional (empty values are allowed). Anchored with ^ and $ for strict full-string matching.
Examples
Input
app=myappMatches
app=myapp
Input
version=v1.2.3Matches
version=v1.2.3
Input
-bad=valueNo match
—