how to use docker image tag parsing regex in javascript?

Question:

I copied this regex for parsing docker image tag in Python.

^(?P<repository>[w.-_]+((?::d+|)(?=/[a-z0-9._-]+/[a-z0-9._-]+))|)(?:/|)(?P<image>[a-z0-9.-_]+(?:/[a-z0-9.-_]+|))(:(?P<tag>[w.-_]{1,127})|)$

Can someone rewrite this regex in Javascript?

Test string:

alpine

alpine:latest

_/alpine

_/alpine:latest

alpine:3.7

docker.example.com/gmr/alpine:3.7

docker.example.com:5000/gmr/alpine:latest

pse/anabroker:latest

The Javascript version here has a pattern error without any matches.

Asked By: Mohammad Javdani

||

Answers:

Your named groups have a different syntax in JS and the / needs escaping

https://regex101.com/r/EpwtjK/1

^(?<repository>[w.-_]+((?::d+|)(?=/[a-z0-9._-]+/[a-z0-9._-]+))|)(?:/|)(?<image>[a-z0-9.-_]+(?:/[a-z0-9.-_]+|))(:(?<tag>[w.-_]{1,127})|)$
Answered By: mplungjan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.