From: Sarah Allen Date: 2009-05-11T21:38:05+09:00 Subject: Re: gsub pattern substitution and ${...} Robert Klemme wrote: > even with proper escaping this won't work as the string interpolation > takes place *before* gsub is invoked and hence URI.escape will insert > something but not the matched portion. In your tests it has probably > worked because $2 was properly set from the previous match. really? so the ${...} gets evaluated before the param is passed to gsub, but the block is passed as code, so then it is evaluated after. Using the previous attempt, with a fresh irb session, I can see the issue: >> $1 => nil >> $2 => nil >> $3 => nil >> require 'URI' => true >> s.gsub(/href=(['"])([^']*)/, "href=\\1#{URI.escape($2)}\\3") NoMethodError: private method `gsub' called for nil:NilClass from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/uri/common.rb:289:in `escape' from (irb):8 >> $1 => nil >> $2 => nil >> $3 => nil >> s = "" => "" >> require 'URI' => true >> s.gsub(/href=(["'])([^'"]+)\1/) { ?> "href=#$1#{URI.escape($2)}#$1" } => "" Nice! > Note that my regexp has another weakness: the quote character not used > to quote the URI should be allowed as part of the URI. I did not want > to complicate things too much but if you want to deal with this the > regular expression must be made a bit more complex. Wow, interesting. That would be incorrect HTML that the browser doesn't deal with well, so I'll not worry about it for this case, but I would be curious how it might be handled. Thanks so much, Sarah -- Posted via http://www.ruby-forum.com/.