根据您的评论,这似乎对您有用:
r = /\[image=(.*?)\](.*?)\[\/image\]/
str = "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"
str.gsub(r,'<div>#{image_tag(\1, alt: \2} <p>\2</p><div>')
#=> "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"