to replace all of the whitespace, or potential newline characters with
null strings.
But I don't think this will work because I really need to loop _within_
each substring of my large HTML string. The thing about gsub is that it
will substitute the entire matched string.
Do I need to scan out the ~^LNK.*?^~, operate on those and then put them
back into the larger string?
I'm not sure I'm asking this very well, so I apologize if that's the
case.
@html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
new_link_line = link_line.gsub(/[\s\r\n]/, '') @html.gsub!(/#{link_line}/mi, new_link_line)
end
This seems to work well:
@html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
new_link_line = link_line.gsub(/[\t\r\n]/, '') @html.gsub!(/#{Regexp.escape(link_line)}/mi, new_link_line) if
link_line != new_link_line
end
I wonder if I could have done with with one @html.gsub!() command, but
this is much more understandable to me anyway so I'll stick with this.
@html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
new_link_line = link_line.gsub(/[\s\r\n]/, '') @html.gsub!(/#{link_line}/mi, new_link_line)
end
This seems to work well:
@html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
new_link_line = link_line.gsub(/[\t\r\n]/, '') @html.gsub!(/#{Regexp.escape(link_line)}/mi, new_link_line) if link_line != new_link_line
end
You can use a block with gsub: @html.gsub!(/~\^LNK:.*?~/mi) { |s| s.gsub /\s/, '' }
Thanks. That is the _Ruby_ way to do it, and that's what I wanted to
know :).
I've used blocks with gsub but I keep forgetting that I can put anything
in there - so far I've only used backrefs to pull out pieces of the
matching regex to rearrange things.