Search code examples
rubystring

Check if String is HTML or not in ruby


How can we check the string is HTML or not using Ruby?

If string contains html tag then returns true otherwise false


Solution

  • If string contains html tag then returns true otherwise false

    You can accomplish this test pretty simply:

    def has_html_tag?(string)
      string =~ /<html.*?>/
    end
    
    has_html_tag?('<html lang="en">foo</html>') #=> true
    has_html_tag?('12345') #=> false
    

    However, this test is not sufficient to determine whether a string is a valid HTML document or a valid HTML fragment.

    How can we check the string is HTML or not using Ruby?

    You can get some basic HTML validation from the Nokogiri gem:

    $ gem install nokogiri
    
    require 'nokogiri'
    
    Nokogiri::HTML.parse("<foo>bar</foo>").errors.empty?