How can we check the string is HTML or not using Ruby?
If string contains html tag then returns true otherwise false
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...>, ...]
If you just want to see if a fragment of html is correct without validating tags:
Nokogiri::XML("<foo>bar</foo>").errors.empty?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With