How to search and replace all urls on a HTML string using RUBY gsub

Hi ,

     I trying to search and replace all urls on a HTML string

using gsub .

CODE

html = “”;

pattern = /<a href=[’"]?([^’"> ])[’"]?[^>]>(.*?)</a>/mo

replace = “#{$2}

html_output = html.gsub(pattern,replace)

The REGEX pattern is apparently working but I 'm not getting the
values of $1 and $2 . When I use \1 and \2 it works .
Thing is … I need to encode $1 variable like this
replace = “#{$2}

and I not able to encode \1

Can anyone help me ?

Try this one

html_output = html.gsub(pattern) do |link|
#{$2}
end

Hi Siddick ,

       Thanks a lot for your response but ...
       It didn't work . html_output outputs the same content as

html variable .

Newton

On Sep 30, 4:07 am, Siddick E. <rails-mailing-l…@andreas-

Based in your solution and searching on internet I tryied the
following aproach and it did work this time .

pattern5 = /<a href=[’"]?([^’"> ])[’"]?[^>]>(.*?)</a>/o

html.gsub!(pattern5) do |n|
#{$2}
end

puts html

I am now able to use $1 and $2

Thanks a lot .

Newton Garcia
Newx - Soluções para Internet
removed_e[email protected]
www.newx.com.br

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs