Remove Certain HTML Tags using Ruby -
how can remove html tags name in ruby?
for example:
string = "<!doctype html><html><body><h1>my first heading</h1><p>my first paragraph.</p></body></html>" string.magic_method("h1") #=> "<!doctype html><html><body><p>my first paragraph.</p></body></html>"
i wrote regex wondered if there library or native method same thing.
using nokogiri:
require 'nokogiri' doc = nokogiri::html <<-_html_ <!doctype html><html><body><h1>my first heading</h1><p>my first paragraph.</p></body></html> _html_ doc.at('h1') # => #(element:0x4d2f006 { # name = "h1", # children = [ #(text "my first heading")] # }) doc.at('h1').unlink puts doc.to_html # >> <!doctype html> # >> <html><body><p>my first paragraph.</p></body></html>
Comments
Post a Comment