Goal is to convert
<img src="<%= ENV["IMAGE_URL"] %>/icons/app-ui-logo-white.png" id="topbar-logo" alt="Freedom logo">
to
<%= image_tag("/static/images/icons/app-ui-logo-white.png", id: "topbar-logo", alt: "Freedom logo") =>
Thanks to @wam for help with this
- each step shows the match string and the replacement string. these were used in SublimeText, YMMV in other engines. the beginning and end
/
are not needed, they are there just for syntax highlighting. - i have the match strings below set as "ruby" syntax, just so i can get the regex highlighting
- these steps don't account for self-closing
img
tags. those are easy to find and clean up in a final step img
tags that have no attributes won't be picked up by this recipe. it will be easy to change it to do that, but I didin't need that when I was making this so I didin't experiment with making it more flexible.
/<img (.*) src="<%= ENV\["IMAGE_URL"\] %>([^"]+)" ([^>]+)>/
<img src="<%= ENV\["IMAGE_URL"\] %>$2" $1 $3>
/<img src="<%= ENV\["IMAGE_URL"\] %>([^"]+)" ([^>]+)>/
<%= image_tag("/static/images$1", $2) %>
/",\s*(\w+)="([^"]+)"/
", $1: "$2",
May need to run multiple times depending on Regex engine/settings.
/,\)/
)