r = /\[image=(.*?)\](.*?)\[\/image\]/
str =<<DOC
I have [image=?1]?2[/image]
with many other [image=?3]?4[/image]
and I need to replace the [image=?1]?2[/image]
with other formatting
DOC
result = str.gsub(r,'<div>#{image_tag(\1, alt: \2} <p>#{\2}</p></div>')
puts result
#=> I have <div>#{image_tag(?1, alt: ?2} <p>#{?2}</p></div>
# with many other <div>#{image_tag(?3, alt: ?4} <p>#{?4}</p></div>
# and I need to replace the <div>#{image_tag(?1, alt: ?2} <p>#{?2}</p></div>
# with other formatting