0

我想在 iframe 中附加 youtube 链接的参数?wmode=transparent, 例如,对于下面的链接:src

<iframe frameborder=\"0\" height=\"315\" src=\"http://www.youtube.com/embed/F4_Bq16rB2Y\" width=\"560\"></iframe>

我想要src=\"http://www.youtube.com/embed/F4_Bq16rB2Y?wmode=transparent\"在 iframe 中。

result = <iframe frameborder=\"0\" height=\"315\" src=\"http://www.youtube.com/embed/F4_Bq16rB2Y?wmode=transparent\" width=\"560\"></iframe> 

使用正则表达式和/或 ruby​​ 方法。

4

2 回答 2

0
str ='<iframe frameborder=\"0\" height=\"315\" src=\"http://www.youtube.com/embed/F4_Bq16rB2Y\" width=\"560\"></iframe>'
puts str.gsub('\" width=', '?wmode=transparent\" width=')

#=><iframe frameborder=\"0\" height=\"315\" src=\"http://www.youtube.com/embed/F4_Bq16rB2Y?wmode=transparent\" width=\"560\"></iframe>
于 2012-05-15T18:24:33.837 回答
0

示例 str 是否包含整个 iframe 文本。您可以gsub像这样使用分组:

str = "<iframe ....>"
str.gsub!(/(youtube[\w\.\/]+)/, '\1?wmode=transparent')
于 2012-05-15T18:08:56.573 回答