newx
September 30, 2009, 8:54am
1
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 ?
newx
September 30, 2009, 9:07am
2
Try this one
html_output = html.gsub(pattern) do |link|
“#{$2} ”
end
newx
September 30, 2009, 3:37pm
3
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 -
newx
September 30, 2009, 6:40pm
4
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
[email protected]
www.newx.com.br