- Split into a regular expression URI; include the URI in the result.
- For each part:
- if it is a URI, leave it alone
- otherwise replace words
- Join the parts
the code:
URI_REGEX = %r"((?:(?:[^ :/?
def replace_except_uris(text, old, new)
text.split(URI_REGEX).collect do |s|
if s =~ URI_REGEX
s
else
s.gsub(old, new)
end
end.join
end
text = <<END
stack http://www.stackoverflow.com stack
stack http://www.somewhere.come/stack?stack=stack
END
puts replace_except_uris(text, /stack/, 'LINKED-LIST')
source
share