Arvind Kumar Arvind Kumar - 5 months ago 32
Ruby Question

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

Answer

If string contains html tag then returns true otherwise false

This test ("string contains <html>") is not sufficient to determine whether a string is HTML.

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

The excellent Nokogiri gem provides HTML validation.

$ gem install nokogiri

require 'nokogiri'

Nokogiri::HTML.parse("<foo>bar</foo>").validate

# => [#<Nokogiri::XML::SyntaxError...>, ...]