Ruby gsub 捕获组

问题描述 投票:0回答:1
ruby-on-rails ruby string replace gsub
1个回答
0
投票

根据您的评论,这似乎对您有用:

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"
© www.soinside.com 2019 - 2024. All rights reserved.