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
React Native How to Call Multiple Functions When Onpress Is Clicked
Useselector Not Updating When Store Has Changed in Reducer. Reactjs Redux
Javascript Form Submit - Confirm or Cancel Submission Dialog Box
How to Look for Changes in Localstorage in Angular 4
How to Format and Display Json Data Using Array.Map in Reactjs
Connecting to Tcp Socket from Browser Using JavaScript
Check Checkbox and Trigger Change Event JavaScript
Filter Input Text to Enter Only Number and Not Even Decimal
How to Close Bootstrap 3 Dropdown When We Click Outside on a Tablet
How to Press Enter Key Automatically Using Jquery
Whenever I Click Outside the Element It Should Close
How to Add Query String Parameter to Same Url When on Click a Href Link
How to Mock a Fake Database for When Unit Testing Against Knex
How to Set Top and Bottom Margin in Addhtml
How to Change Font-Size in Vuetify Component
Javascript and Getelementbyid for Multiple Elements With the Same Id
How to Stop Parsefloat() from Stripping Zeroes to Right of Decimal