javascript - Check if a text is HTML or not -


i using meteor , trying check if text html. usual ways not work. code:

post: function() {     var postid = session.get("postid");     var post = posts.findone({         _id: postid     });     var object = new object();     if (post) {         object.title = post.title;         if ($(post.content).has("p")) { //$(post.content).has("p") / post.content instanceof htmlelement               object.text = $(post.content).text();               if (post.content.match(/<img src="(.*?)"/)) {                 object.image = post.content.match(/<img src="(.*?)"/)[1];             }         } else {             console.log("it not html------------------------");             object.text = post.content;         }     }     return object; } 

actually, "working" solution have used now. also, pointed out 2 common ways use (next if statement). possible happen without regex.

can use approach started jquery append response new <div> , check if element has children. if jquery finds children html.

if html can search div type of element using find().

// create element , inject content var $div=$('<div>').html(post.content); // if there children html if($div.children().length){    console.log('this html');    var $img = $div.find('img');    console.log('there ' + $img.length +' image(s)'); }else{    console.log('this not html'); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -