I need to RegEx that I am using in to find strings to replace via a Grunt task.
I am wanting to swap out locally referenced image URLs with their external URLs. The file structure is the same both locally and externally.
I need to look for /img
or img
at the start of a URL (either in an img
tag's src
attribute, or CSS rules) and swap in the external URL.
E.g. look for /img/some-photo.jpg
or img/some-photo.jpg
and replace /img
or img
with https://www.example.com/some-photo.jpg
.
I can't just look for img
alone as that would also match the string in the following HTML <img .../>
turning the tag into <https://www.example.com .../>
.
I can exclude the img
tag like this:
/\/img|(^|[^\<])img/gim
But that also matches, for example:
(img
'img
"img
etc.
I don't want to exclude the string img
in these examples, I just don't also want to capture the preceding characters ((
, '
, "
, etc.)
You can see this in action here: https://regexr.com/3qbeq