Skip to content

Instantly share code, notes, and snippets.

@ileitch
Created July 15, 2010 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ileitch/476406 to your computer and use it in GitHub Desktop.
Save ileitch/476406 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'nokogiri'
html = %q{blah http://google.com blah blah}
new_html = %q{blah <a href="http://google.com">http://google.com</a> blah blah}
frag = Nokogiri::HTML.fragment(html)
text = frag.children.first
p frag.to_html
text.replace(Nokogiri::HTML.fragment(new_html))
p "-" * 10
p frag.to_html
======
Output:
Desktop $ ruby nokogiri_replace.rb
"blah http://google.com blah blah"
"----------"
"<a href=\"http://google.com\">http://google.com</a>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment