I am new to coffee script and my end goal is to change the value of src in image tag to a different one. the input will be a string.
lets say
string x = '<div class="sample">
<img src="/i/java.png">
</div>
<div class="sample">
<img src="/i/python.png">
</div>'
I want to replace the contents of src to something else. I tried with the regex to try it out but it does not work. Any idea on how do i achieve this. I used this regex.
s.replace /[/"][//]{1}i[//]{1}/g, '"//cdn.example.com/'
i am using my local application as well as this website to test my code
I believe there is a problem with your original regex.
Give this example a try,
x = '<div class="sample">
<img src="/i/java.png">
</div>
<div class="sample">
<img src="/i/python.png">
</div>'
console.log(x.replace /\/i\/[a-zA-Z1-9]+.png/g, '"//cdn.example.com/')
The regex /\/i\/[a-zA-Z1-9]+.png/g
should match any values with the format of /i/anything_here.png
, but ensures that the anything_here
value contains at least 1 character (so /i/.png
won't match).
If your string might contain more subpaths before the .png
filename, use the following regex - \/i(\/[a-zA-Z1-9]+)+.png
This regex will allow as many occurrences of /anything
before the /filename.png
.