Check If a String Is HTML or Not

Check if a string is html or not

A better regex to use to check if a string is HTML is:

/^/

For example:

/^/.test('') // true
/^/.test('foo bar baz') //true
/^/.test('<p>fizz buzz</p>') //true

In fact, it's so good, that it'll return true for every string passed to it, which is because every string is HTML. Seriously, even if it's poorly formatted or invalid, it's still HTML.

If what you're looking for is the presence of HTML elements, rather than simply any text content, you could use something along the lines of:

/<\/?[a-z][\s\S]*>/i.test()

It won't help you parse the HTML in any way, but it will certainly flag the string as containing HTML elements.

How to check if string contents have any HTML in it?

If you want to test if a string contains a "<something>", (which is lazy but can work for you), you can try something like that :

function is_html($string)
{
return preg_match("/<[^<]+>/",$string,$m) != 0;
}

Check if String is HTML or not in ruby

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...>, ...]

Check if a String is HTML or XML

Is there a way to check if a String is HTML or XML in JavaScript?

Not reliably. You can test for one (e.g. against a DTD or XSD) and if it fails, assume it's the other. However, those tests are meant to be run on entire documents with a valid DOCTYPE. There are many cases where a snippet of markup will pass validation for multiple markup languages. What then?

I think you need to explain why you need to know the difference.

How to identify if a text is HTML or not? (in PHP)

you can only check for chars specific for html in string

function is_html($string)
{
return preg_match("/<[^<]+>/",$string,$m) != 0;
}

jQuery Check if String is HTML Tag

I found the problem myself... It was a logical error...

the point is that when the string is a normal text, we can take it with using text(), but when it is HTML we should use html() instead, otherwise the text() returns null...

I've also learned something from you guys... Thank you all very much... ;)



Related Topics



Leave a reply



Submit