this is my string
<img class="img" src="a.png"><img class="img" src="a.png"><img class="img" src="a.png">
i want to check if string contain only html tags
dwd<img class="img" src="a.png">dwd<img class="img" src="a.png"><img class="img" src="a.png"> dwd
if contain any string like example above i want to return false
i have some code here for check about thats
function isHTML(str) {
var a = document.createElement('div');
a.innerHTML = str;
for (var c = a.childNodes, i = c.length; i--; ) {
if (c[i].nodeType == 1) return true;
}
return false;
}
isHTML('<a>this is a string</a>') // true
isHTML('this is a string') // false
isHTML('this is a <b>string</b>') // true
as we can see in third example its return true and there is some string with html tags so how can i edit that's and make it return true if only there are html tags none text
another method here but same above
var isHTML = RegExp.prototype.test.bind(/(<([^>]+)>)/i);
isHTML('Testing'); // false
isHTML('<p>Testing</p>'); // true
isHTML('<img src="hello.jpg">'); // true
isHTML('My <p>Testing</p> string'); // true (caution!!!)
isHTML('<>'); // false
its good method but isHTML('My <p>Testing</p> string'); // true (caution!!!)
here i want to return false because there is some string with the html tags
return false;as soon as you see a child node that's not type 1, and thenreturn trueat the end.function isHTMLONLY(str) { var a = document.createElement('div'); a.innerHTML = str; for (var c = a.childNodes, i = c.length; i--; ) { if (c[i].nodeType != 1) return false; } return true; }childNodesincludes text nodes...type is 3, html comments are 9 if interested in those