Reputation: 2293
I have a set of HTML, where I am looking to scan for each instance of "img src" and replace the value of "src", which is in this instance "cid:ii_151afcf74cab7d3b" with a variable. The content of "src" will vary for each instance. How would I do this with a regular expression?
<img src="cid:ii_1w1afcd4cab7d3b" alt="Inline image 1" width="517" height="291">
Upvotes: 0
Views: 68
Reputation: 6100
Expression:
/(?<=src=")([^"]+)/
will match your src
content without the "
sign
(?<=src=")
is positive look-behind, will match but not take into consideration
([^"]+)
match anything except "
, it will match everything you throw at it inside that your src="will_match_whatever_except_double_quotes"
Upvotes: 2
Reputation: 213
You can do interpolation in regular expressions just like you can in strings and symbols:
str = 'foo<img src="bar" alt="Inline image 1" width="517" height="291">'
src = /<img src="#{src}" alt="Inline image 1" width="517" height="291">/
str =~ src #=> 4
Edit: I forgot that since you are looking to replace, you will need to use String#gsub
/String#gsub!
or if you want to do something fancy, maybe String#scan
Upvotes: 1