const regex = /^(?<Name>(?<=^)(?:(?<Domain>(?:(?:localhost|[\w-]+(?:\.[\w-]+)+)(?::\d+)?)|[\w]+:\d+)\/)?\/?(?<Namespace>(?:(?:[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)\/)*)(?<Repo>[a-z0-9-]+))[:@]?(?<Reference>(?<=:)(?<Tag>[\w][\w.-]{0,127})|(?<=@)(?<Digest>[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][0-9A-Fa-f]{32,}))?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<Name>(?<=^)(?:(?<Domain>(?:(?:localhost|[\\w-]+(?:\\.[\\w-]+)+)(?::\\d+)?)|[\\w]+:\\d+)\\\/)?\\\/?(?<Namespace>(?:(?:[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*)\\\/)*)(?<Repo>[a-z0-9-]+))[:@]?(?<Reference>(?<=:)(?<Tag>[\\w][\\w.-]{0,127})|(?<=@)(?<Digest>[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][0-9A-Fa-f]{32,}))?', 'gm')
const str = `alpine
alpine:latest
localhost/latest
library/alpine
localhost:1234/test
test:1234/blaboon
alpine:3.7
docker.example.edu/gmr/alpine:3.7
docker.example.com:5000/gmr/alpine@sha256:5a156ff125e5a12ac7ff43ee5120fa249cf62248337b6d04abc574c8
docker.example.co.uk/gmr/alpine/test2:latest
registry.dobby.org/dobby/dobby-servers/arthound:2019-08-08
owasp/zap:3.8.0
registry.dobby.co/dobby/dobby-servers/github-run:2021-10-04
docker.elastic.co/kibana/kibana:7.6.2
registry.dobby.org/dobby/dobby-servers/lerphound:latest
registry.dobby.org/dobby/dobby-servers/marbletown-poc:2021-03-29
marbles/marbles:v0.38.1
registry.dobby.org/dobby/dobby-servers/loophole@sha256:5a156ff125e5a12ac7ff43ee5120fa249cf62248337b6d04abc574c8
sonatype/nexon:3.30.0
prom/node-exporter:v1.1.1
sosedoff/pgweb@sha256:5a156ff125e5a12ac7ff43ee5120fa249cf62248337b6d04abc574c8
sosedoff/pgweb:latest
registry.dobby.org/dobby/dobby-servers/arpeggio:2021-06-01
registry.dobby.org/dobby/antique-penguin:release-production
dalprodictus/halcon:6.7.5
antigua/antigua:v31
weblate/weblate:4.7.2-1
redis:4.0.01-alpine
registry.dobby.com/dobby/dobby-servers/github-run:latest`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions