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.
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})|)$
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.
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})|)$