I'm implementing the srcset attribute on some images within a site and hit an issue because Chrome doesn't like srcset image urls that contain spaces. My source HTML has a number of image tags which look like:
<img src="/images/image 1.jpg" alt="My alt" sizes="100vw" srcset="/images/image 1 136.jpg 136w, /images/image 1 165 165w, /images/image 1 286 286w">
What I'm wanting to do is to replace the spaces in the srcset with %20 so it looks like:
<img src="/images/image 1.jpg" alt="My alt" sizes="100vw" srcset="/images/image%201%20136.jpg 136w, /images/image%201%20165 165w, /images/image%201%20286 286w">
To simplify the problem - if I can replace the n spaces in the src on the following then I can fix my problem:<img src="/images/image any number of spaces.jpg" />
I think the only solution would be using lookbehind but as I'm doing this in javascript its not supported so I'm stumped. I would normally use something like:myText.replace(/src=".[^"]*"/gi,'%20')
but this would replace the whole string. See this example.
I suspect this problem can't be solved (and it is only for a temporary measure while we test out the impact of srcset after which I can fix it at source) - but I don't know if there is something I'm not thinking of that would enable me to achieve what I want.